【发布时间】:2015-07-07 05:49:21
【问题描述】:
我正在尝试开发小型项目。我正在使用 webview 活动,但是如何获取 webview 内容(HTML 文件)并传递给电子邮件正文.....?
【问题讨论】:
标签: android android-intent android-fragments
我正在尝试开发小型项目。我正在使用 webview 活动,但是如何获取 webview 内容(HTML 文件)并传递给电子邮件正文.....?
【问题讨论】:
标签: android android-intent android-fragments
为了将 webview 内容作为 html,拍摄 webview 的快照并将图像文件附加到邮件中。
将 Web 视图作为屏幕截图(图片)
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = mWebview.getRootView(); // take the view from your webview
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
通过电子邮件发送附件
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile="temp/attachement.xml";
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
return;
}
Uri uri = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));
您还需要通过如下清单文件授予用户权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
【讨论】:
这就是您从WebView 获得html 的方式:
定义一个JavascriptInterface 类
class MyJavaScriptInterface {
MyJavaScriptInterface() {
}
@SuppressWarnings("unused")
@JavascriptInterface
public void getHTML(String html) {
// send the email with the html
}
}
然后获取您的 WebView 并进行设置...
WebView webview = (WebView) findViewById(R.id.yourid);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "INTERFACE");
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webview.loadUrl("javascript:window.INTERFACE.getHTML(document.documentElement.innerHTML);");
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view,url);
return false;
}
});
webview.loadUrl("your url");
记得通过如下清单文件授予用户权限
<uses-permission android:name="android.permission.INTERNET" />
【讨论】: