0

I am using the following code to get a Bitmap image of the html webpage that is loaded in WebView, however getting crashes due to running out of memory. how do i avoid memory problems if it is directly converting the picturedrawable into a Bitmap.

Is there a way to load file directly into a compressed format lke jpg? or is there some other way to handle this problem?

using Bitmapfactory to downsize the image does not look like it is possible, because the Bitmap is already created so it would be too late to do anything about it.

    wv = (WebView)findViewById(R.id.webView1);
    wv.loadUrl("http://www.google.com");
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);

    Picture picture = wv.capturePicture();

   HTMLBitmap = pictureDrawable2Bitmap(picture);
        Toast.makeText(HTMLActivity.this, "bitmap " + HTMLBitmap.toString(), Toast.LENGTH_LONG).show();
         
       Bitmap bmp = Bitmap.createScaledBitmap(HTMLBitmap, 500, 300, false);

    // the imageView that is being set to the bitmap                
   imageOne.setImageBitmap(bmp);

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

EDIT:

two possible solutions

1

one idea I just thought of is somehow creating the Image on external memory like the SD card and using BitmapFactory to drop the size if it is large, before putting on the internal memory, how to actually do that is another question. the bitmap would have to be originally created on the external memory.

2

or is the memory problem only caused by loading it into an imageView. In that case I can drop the image with BitmapFactory Options before loading it in the imageView.

Community
  • 1
  • 1
Kevik
  • 9,181
  • 19
  • 92
  • 148
  • you have some nice discussions in SO http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Lochana Ragupathy Mar 22 '13 at 07:17

1 Answers1

0

Bitmap bmp = Bitmap.createScaledBitmap(HTMLBitmap, 500, 300, false);

Kevik
  • 9,181
  • 19
  • 92
  • 148