网页流量捕获导致内存溢出异常 [英] webview capturing causes out of memory exception

查看:218
本文介绍了网页流量捕获导致内存溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载HTML数据到web视图,在那之后我需要捕捉的WebView数据和图像存储到SD卡。对于小图像的做工精细,但它给出大图内存异常。我使用下面的逻辑来做到这一点。

i am loading html data into webview , after that i need to capture webview data and stored that image into sd card. Its working fine for small images, but it gives out of memory exception for big images. i am using the following logic to do this.

private void generateImg() {
    // TODO Auto-generated method stub
      try{

          Picture p = webview.capturePicture();
           Bitmap bitmap=pictureDrawable2Bitmap(p);
            String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
            File myDir = new File(root + "/Aack");
            if(myDir.exists())
             {
                //Log.e("Directory","Existed");
             }
             else
             {
                myDir.mkdir();
             }
             String fname = System.currentTimeMillis()+".png";
             //Log.e("file name...",""+fname);
             file = new File (myDir, fname);
             try 
             {
                 FileOutputStream out = new FileOutputStream(file);
                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                 out.flush();
                 out.close();

             }
             catch (Exception e) 
             {
                    e.printStackTrace();
             }
             file_name=myDir+"/"+fname;


       }catch(Exception e)
       {
        e.printStackTrace();
       }

}




private static Bitmap pictureDrawable2Bitmap(Picture picture){
        PictureDrawable pictureDrawable = new PictureDrawable(picture);
        Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pictureDrawable.getPicture());
        return bitmap;
    }

所以,请指导我如何处理这个问题。谢谢

so, please guide me how to handle this. Thank you

推荐答案

像@varan说,尽量去code图像:

like @varan said, try to decode the image:

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
}

    public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;

        int stretch_width = Math.round((float)width / (float)reqWidth);
        int stretch_height = Math.round((float)height / (float)reqHeight);

        if (stretch_width <= stretch_height) return stretch_height;
        else return stretch_width;
}

我不知道该怎么做你的特殊情况下,从图片为位图,因此,如果其他人知道随意编辑这篇文章。

I don't know how to do it in your special case from Picture to Bitmap, so if someone else knows feel free to edit this post.

这篇关于网页流量捕获导致内存溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆