0

I have created android application which has images which I am decoding from URL. I am getting following error while decoding URL into Bitmap. Its occurring on random phones. What is best case the error doesnt occur.

Following is error:

Caused by: java.lang.OutOfMemoryError
       at android.graphics.BitmapFactory.nativeDecodeStream(BitmapFactory.java)
       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:663)
       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:735)

This is code i used to decode image url:

  Bitmap mIcon11 = null;
String filePath = imageURL;
InputStream in = null;
in = new java.net.URL(filePath).openStream();
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 8;
o.inJustDecodeBounds = true;
o.inPurgeable = true;
mIcon11 = BitmapFactory.decodeStream(in);
return mIcon11; 

I don't want it to crash in any case.

Please help

virendrao
  • 1,763
  • 2
  • 22
  • 42
  • Before doing anything with bitmap you have to go through android's Loading Large Bitmaps Efficiently section http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Praveena Nov 09 '14 at 06:21
  • Similar issue here http://stackoverflow.com/questions/20441644/java-lang-outofmemoryerror-bitmapfactory-decodestrpath – SMA Nov 09 '14 at 06:22
  • @almasshaikh i have inserted that for recycling and released. Let see – virendrao Nov 09 '14 at 06:36

3 Answers3

0

You can go with universial image loader liabrary. It is having good handling for images also provide cache for them. https://github.com/nostra13/Android-Universal-Image-Loader

You can use:- ImageLoader.getInstance().loadImageSync(uri, targetImageSize, options);

VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
  • I am using Picasso and I have provided option to set image as wallpaper where I need to decode URL into bitmap which is input to method. – virendrao Nov 09 '14 at 12:38
  • I think Universal Image Loader have very good implementation for handling of bitmap try to solve problem with that. –  Nov 10 '14 at 01:14
0

Hi vo12 USE Aquery (https://code.google.com/p/android-query/wiki/ImageLoading) or universal library(https://github.com/nostra13/Android-Universal-Image-Loader) to manage the decoding the image or managing images these are best in my opinion i also faced similar issue i used these and now outofmemory error is rare.

Piyush
  • 1,973
  • 1
  • 19
  • 30
  • I am using Picasso and I have provided option to set image as wallpaper where I need to decode URL into bitmap which is input to method. – virendrao Nov 09 '14 at 12:37
0

In my case i had fixed this type of error by reducing the WIDTH and HEIGHT of the image.It might be in your case too and error was same as yours "Caused by: java.lang.OutOfMemoryError".carefully go through your all images u have used in your application, it might be some of images will have different width/height than others images.I think your will crash with some specific device,not with all android devices(because i my scenario,my app was not able to open with samsung s3,s4 note 2,xperia c etc,but all doing good). Actually its cause by images is not adjustable on to the device screen.

Rana Pratap Singh
  • 867
  • 12
  • 18
  • Hi images size are same. Actually I am setting wallpaper and it needa bitmap object as input so while decoding it fails – virendrao Nov 09 '14 at 12:36