如何将WebView显示页面内容发送到android中的电子邮件正文?

时间:2015-04-28 11:37:12

标签: android android-intent android-fragments

我正在尝试开发小项目。我正在使用webview活动但是如何获取webview内容(HTML文件)并传递给Email body .....?

2 个答案:

答案 0 :(得分:2)

将网页视图内容视为html,拍摄网页视图的快照并将图像文件附加到邮件中。

将网页视图作为屏幕截图(图像)

// 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" />

答案 1 :(得分:1)

这是您从html获得WebView的方式:

定义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" />