从webview捕获的图像没有在android中完全加载

时间:2015-03-30 11:45:59

标签: android webview bitmap

我有WebView一堆带有标题和日期的图片。

但是,捕获webview后,结果图像未完全加载。

我的代码在4.3中工作,但在4.4及以上版本中不起作用

我该如何解决?

生成位图并保存到SD卡:

myWebView.getHeight();
Picture p = myWebView.capturePicture();
myWebView.setDrawingCacheEnabled(true); 
new bitmap(p).execute();

异步类:

class bitmap extends AsyncTask<URL, Void, Bitmap> {
    Picture pict;
    ProgressDialog progressDialog = null;
    public bitmap(Picture pict){
        this.pict=pict;
    }

    protected void onPreExecute() {
    }
    @Override
    protected Bitmap doInBackground(URL... arg0) {
        PictureDrawable pictureDrawable = new PictureDrawable(pict);

        Bitmap bitmap = null;
        bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);

        myWebView.buildDrawingCache();
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        int iHeight = bitmap.getHeight();
        canvas.drawBitmap(bitmap, 0, iHeight, paint);
        myWebView.draw(canvas);

        return bitmap;
    }

    @SuppressLint("NewApi")
    protected void onPostExecute(Bitmap result) {
        myWebView.setDrawingCacheEnabled(false);
        if (result == null) {
            Toast.makeText(MainActivity.this,"Bitmap Null", Toast.LENGTH_LONG).show();
        }
        else {
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root + "/Sample");
            myDir.mkdirs();

            String fname = "sampleimage"+".jpg";
            File file = new File(myDir, fname);
            if (file.exists())
            file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                result.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

和webview数据的代码:

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

String samplehtml="<html><head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes' /><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> </head>   <body ><img alt='Embedded Image'  style='min-height:30%;width:100%;height:23%;border-radius: 0px;margin-bottom:5%;' src='file:///android_asset/images.jpg'/></div></div><br><br><br><br>";

for(int i=0;i<55;i++) {
    samplehtml+="<img alt='Embedded Image'  style='min-height:30%;width:100%;height:23%;border-radius: 0px;margin-bottom:5%;' src='file:///android_asset/images.jpg'/><p style='color:black;width:100%;float:right;'>Sample text</p><br><br></div><p id='dateleft' style='margin-top:3%;'>sample date</p></div><br><br><br><br></body></html>";
}

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        myWebView.getHeight();
        Picture p = myWebView.capturePicture();
        myWebView.setDrawingCacheEnabled(true); 
        new bitmap(p).execute();
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
    }
});
myWebView.loadDataWithBaseURL(null, samplehtml, null, "utf-8",null);

0 个答案:

没有答案