3

I am using following code to display bitmap in my ImageView. When I try to load image of size for example bigger than 1.5MB it give me error. Any one suggest me solution?

  try {  

                         URL aURL = new URL(myRemoteImages[val]);  
                         URLConnection conn = aURL.openConnection(); 

                         conn.connect();  
                         InputStream is = null;
                         try
                         {
                             is= conn.getInputStream();  
                         }catch(IOException e)
                         {


                             return 0;

                         }
                         int a=  conn.getConnectTimeout();
                         BufferedInputStream bis = new BufferedInputStream(is);  

                         Bitmap bm;
                         try
                         {
                             bm = BitmapFactory.decodeStream(bis);
                         }catch(Exception ex)
                         {
                             bis.close(); 
                             is.close();  
                             return 0; 
                         }
                         bis.close();  
                         is.close();  
                         img.setImageBitmap(bm);

                    } catch (IOException e) {  
                        return 0;
                    }  

                    return 1;

Log cat:

06-14 12:03:11.701: ERROR/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
06-14 12:03:11.861: ERROR/AndroidRuntime(443): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-14 12:03:11.861: ERROR/AndroidRuntime(443):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
XXX
  • 8,996
  • 7
  • 44
  • 53
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

3 Answers3

1

You should decode with inSampleSize option to reduce memory consumption. Strange out of memory issue while loading an image to a Bitmap object

Another option inJustDecodeBounds can help you to find correct inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

Community
  • 1
  • 1
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory. BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType; – hitesh141 Apr 08 '15 at 05:35
0

In general I think this blog covers the best practices on how to watch memory allocation/ how to use Weak/Soft References to avoid overflows. Hope this helps.

Codevalley
  • 4,593
  • 7
  • 42
  • 56
-2
try {
    Bitmap bitmap=null;

    byte[] profileImageInBytes;

    String url="http://photo.net/learn/collage/complete-full-size.jpg";

    HttpGet httpRequest = null;

    httpRequest = new HttpGet(url);

    HttpClient httpclient = new DefaultHttpClient();

    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

    HttpEntity entity = response.getEntity();

    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

    InputStream instream = bufHttpEntity.getContent();

    System.gc();

    Runtime.getRuntime().gc();

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    bmpFactoryOptions.inTempStorage = new byte[32 * 1024];
    bmpFactoryOptions.inSampleSize = 4;     
    bmpFactoryOptions.outWidth = 640;      
    bmpFactoryOptions.outHeight = 480;     
    bmpFactoryOptions.inDither=false;          
    bmpFactoryOptions.inInputShareable=true; 

    bitmap = BitmapFactory.decodeStream(instream, new Rect(), bmpFactoryOptions);

    System.out.println("hi " +bitmap);
    Bitmap map = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
    System.out.println("23");
    System.out.println("hihi hi " +map);
    BitmapDrawable bmd = new BitmapDrawable(map);

    System.out.println("24");
    System.out.println("hihi hi " +bmd);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    System.out.println(stream);

    map.compress(Bitmap.CompressFormat.JPEG, 100, stream);

    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
      / (float) 400);
    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
      / (float) 400);

    if (heightRatio > 1 || widthRatio > 1) {
     if (heightRatio > widthRatio) {
      bmpFactoryOptions.inSampleSize = heightRatio;
     } else {
      bmpFactoryOptions.inSampleSize = widthRatio;
     }
    }

    Bundle params=new Bundle();
    params.putString("method", "photos.upload");
    profileImageInBytes = stream.toByteArray();
    System.out.println(profileImageInBytes);
    System.out.println(" profile image bytes ");
    System.out.println("Bytes : " + profileImageInBytes);
    params.putByteArray("picture", profileImageInBytes);
    System.out.println("My Picture : " + params);

    mAsyncRunner.request(null, params, "POST",
            new SampleUploadListener(), null);
    System.out.println("Uploading");

}
catch  (IOException e) {
    e.printStackTrace();
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Mohan
  • 5
  • 1