【问题标题】:How do I send WebView display page content to email body in android? [closed]如何将 WebView 显示页面内容发送到 android 中的电子邮件正文? [关闭]
【发布时间】:2015-07-07 05:49:21
【问题描述】:

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

【问题讨论】:

    标签: android android-intent android-fragments


    【解决方案1】:

    为了将 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" />
    

    【讨论】:

    • 感谢重播...我的意图是当我单击 shareIT 按钮(按钮下方的 Web 视图)时。然后将附加到正文的所有显示信息移动为附加文件...... ........
    【解决方案2】:

    这就是您从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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 2012-06-27
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多