Reputation: 23
I have built a WebView application that stores some information in DOM storage (localstorage). As part of the application, I would like to export that data either to a .csv file or to an email (attached as .csv, etc).
I'm able to get the data in a .csv format, but I'm not sure how to export or send an email with the data.
Upon clicking the "Export" button, I have tried the following:
var currentData = getHistorySummary("myObject");
var dataArray = [];
for (var i = 0; i < currentData.length; i++) {
dataArray.push(JSON.parse(currentData[i]));
}
dataArray.sortBy('id');
var csv = convertJSONtoCSV(dataArray);
var csvContent = "data:text/csv;charset=utf-8,";
csvContent += csv;
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
window.open(encodedUri);
However nothing happens at this point, and I receive the following warning:
Cannot call determinedVisibility() - never saw a connection for the pid: 27146
Is there a better way to "export" this information, or am I missing something?
Upvotes: 0
Views: 874
Reputation: 23
I ended up doing the following (the .csv content gets sent in the body of the email):
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("data:text/csv")) {
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:[email protected]"));
i.putExtra(android.content.Intent.EXTRA_TEXT, URLDecoder.decode(url));
startActivity(i);
}
return true;
}
});
It's not ideal, but for the project I'm working on, it will do.
Upvotes: 1
Reputation: 69338
You can use [WebView#addJascriptInterface()](http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)) and publish a method that sends the email, probably also passing the data in the localstorage too.
Upvotes: 0
Reputation: 505
Try this by giving your file name and file path-
try
{
String fileName = URLEncoder.encode(yourfilename, "UTF-8");
String PATH = Environment.getExternalStorageDirectory()+"/"+fileName.trim().toString();
Uri uri = Uri.parse("file://"+PATH);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, "");
i.putExtra(Intent.EXTRA_SUBJECT,"android - email with attachment");
i.putExtra(Intent.EXTRA_TEXT,"");
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("text/csv");
context.startActivity(Intent.createChooser(i, "Select application"));
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
source linkhere
Upvotes: 0