【问题标题】:Android:how to convert html code into image and send this image using ShareIntent?Android:如何将 html 代码转换为图像并使用 ShareIntent 发送此图像?
【发布时间】:2015-07-06 05:29:11
【问题描述】:

我使用 webView 将内容显示和捕获为图像并使用共享选项发送。
但我需要在不使用 webview 的情况下将 html 代码转换为图像。
请帮帮我。
提前致谢。

【问题讨论】:

  • 我们可以查看您的代码以及您在哪里发现了这个问题?
  • Hello Micho...下面我添加了使用 webView 将 html 代码转换为图像并使用 ShareIntent 共享此图像的代码。
  • 这能回答你的问题吗? Generate bitmap from HTML in Android

标签: android android-intent android-bitmap


【解决方案1】:

我已经使用 webView 完成了这项工作。

webview = (WebView) findViewById(R.id.webView1);

WebSettings settings = webview.getSettings();
settings.setBuiltInZoomControls(true);
settings.setUseWideViewPort(false);
settings.setJavaScriptEnabled(true);
settings.setSupportMultipleWindows(false);

settings.setLoadsImagesAutomatically(true);
settings.setLightTouchEnabled(true);
settings.setDomStorageEnabled(true);
settings.setLoadWithOverviewMode(true);

WebView webview = (WebView) findViewById(R.id.webview);
WebView.enableSlowWholeDocumentDraw();

String RESULTDATA = "<html><body><h1>It's working</h1></body></html>";
if (!RESULTDATA.equals(null)) {
    Log.e("info", RESULTDATA);
    webview.loadData(RESULTDATA, "text/html", null);
}

circleButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        shareResultAsImage(webview);
    }
});

shareResultAsImage(WebView)方法需要定义。

private void shareResultAsImage(WebView webView) {
    Bitmap bitmap = getBitmapOfWebView(webView);
    String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "data", null);
    Uri bmpUri = Uri.parse(pathofBmp);
    final Intent emailIntent1 = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent1.setType("image/png");
    emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);

    startActivity(emailIntent1);
}

private Bitmap getBitmapOfWebView(final WebView webView) {
    Picture picture = webView.capturePicture();
    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);
    picture.draw(canvas);
    return bitmap;
}

【讨论】:

  • 很好的答案!谢谢。
  • 这正是我所需要的,但是,我注意到从 webview 生成的图像质量下降,其中包含的文本变得有点模糊,知道为什么会这样发生了什么?
【解决方案2】:

可能有许多可能的解决方案。您可以通过显示全屏 Web 视图并以编程方式捕获设备的屏幕截图并通过后台服务发送数据来以某种方式实现它。
用于通过程序截取屏幕截图;您可以使用以下链接:
http://amitandroid.blogspot.in/2013/02/android-taking-screen-shots-through-code.html

【讨论】:

    【解决方案3】:

    查看此 SO 答案 https://stackoverflow.com/a/64853392/4944007。其中使用的库在从 HTML 生成位图方面确实有很大帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-19
      • 2017-12-01
      • 2016-12-16
      • 2018-11-24
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      相关资源
      最近更新 更多