【问题标题】:webview capturing causes out of memory exceptionwebview 捕获导致内存不足异常
【发布时间】:2013-12-09 18:44:30
【问题描述】:

我正在将 html 数据加载到 webview 中,之后我需要捕获 webview 数据并将该图像存储到 sd 卡中。它适用于小图像,但它会为大图像提供内存不足异常。我正在使用以下逻辑来执行此操作。

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;
    }

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

【问题讨论】:

  • 我想知道小图和大图是什么意思,是加载到webView还是抓图?
  • 在哪一行抛出异常?
  • 位图位图 = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); @Bharat Sharma
  • 同时捕捉大图像。 @vinayKumar
  • 在保存或创建缩放位图之前尝试解码图像

标签: android memory webview bitmap


【解决方案1】:

就像@varan 说的,尝试解码图像:

    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;
}

我不知道如何在你的特殊情况下从图片到位图,所以如果其他人知道,请随时编辑这篇文章。

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 2012-12-15
    • 2011-05-26
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 2016-02-22
    相关资源
    最近更新 更多