1

in my app i have an option to show the images in sd card, from that the user can select one image and it will be loaded in the the image view of my next activity.

I have tested the app in two of my devices such as Samsung Ace and Samsung Galaxy S3.

In Samsung Ace of 2.3 version, i am able to select the image and from onActivity result the next Activity is called and the image gets loaded in Imageview, here i am able to do this for n number of times,(other option i use is capture via camera and load in next activity, here too the process is good)

When i tried the same process in Samsung Galaxy S3, of version 4.2, for the first time i am able to select or capture via camera and load it in imageview. When i tried for the second time the app gets crashed, saying that

E/dalvikvm-heap( 7058): Out of memory on a 31961104-byte allocation.
E/AndroidRuntime( 7058): FATAL EXCEPTION: main
E/AndroidRuntime( 7058): java.lang.OutOfMemoryError
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
E/AndroidRuntime( 7058): at com.tribal.pages.app.UploadPhoto$ShowCapturedImage.onPostExecute(UploadPhoto.java:442)
E/AndroidRuntime( 7058): at com.tribal.pages.app.UploadPhoto$ShowCapturedImage.onPostExecute(UploadPhoto.java:1)
E/AndroidRuntime( 7058): at android.os.AsyncTask.finish(AsyncTask.java:602)
E/AndroidRuntime( 7058): at android.os.AsyncTask.access$600(AsyncTask.java:156)
E/AndroidRuntime( 7058): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
E/AndroidRuntime( 7058): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 7058): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 7058): at android.app.ActivityThread.main(ActivityThread.java:4517)
E/AndroidRuntime( 7058): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 7058): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 7058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
E/AndroidRuntime( 7058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
E/AndroidRuntime( 7058): at dalvik.system.NativeStart.main(Native Method)
E/android.os.Debug( 2098): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error
E/widget  ( 2468): [MSC_HERO_Accu]>>> SM:399 [0:0] IR : false, IPR : false
E/Launcher( 2468): Error finding setting, default accessibility to not found: accessibility_enabled
E/widget  ( 2468): [MSC_HERO_Accu]>>> SM:399 [0:0] IR : false, IPR : false

I tried using following 2 methods to load the captured image or selected image in ImageView Method 1

Bitmap  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(Appconstants._uri));
                       bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ImageView uplaod_image.setImageBitmap(bitmap);

Method 2

Bitmap bitmap = BitmapFactory.decodeFile(Appconstants.f);
ImageView uplaod_image.setImageBitmap(bitmap);

But everytime the app crashes saying the above error, how to resolve this....

Siva K
  • 4,968
  • 14
  • 82
  • 161

2 Answers2

2

The difference you see in behaviour is probably due to differences in sizes of the images on the two phones.

You might want to call,

upload_image.setImageDrawable(null);

to clear the imageView (or use one of the ways detailed here), before trying to load another image, so that that memory is cleaned up and collected by the Garbage Collector before subsequent allocation and prevent an Out of memory error.

Community
  • 1
  • 1
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
1

Try to scale the image before loading it into the memory according the the screen size that device is having,

You can use BitmapFactory Options inJustDecodeBounds = true to get the size of the Bitmap before decoding it into memory.

get height and width,

int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

Then you can scale it with the required height and width of the image as per your requirement,

int inSampleSize = Math.min(imageWidth /reqWidth,imageHeight /reqHeight);

And then finally apply the inSampleSize to scale the Bitmap. For, further details check my answer here.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242