-2

I have an app which I share images to. Usually it works just fine, but after 5 or 6 shares the app crashes when I try and share another image to it and the error im getting is the OutOfMemory Exception. After the crash the sharing works just fine for about 5-6 shares, and again crashing. My question is how to correct this. Here is the Error:

09-14 18:28:59.290 2224-2224/com.example.user.aitzik4 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
    at android.graphics.drawable.Drawable.createFromPath(Drawable.java:934)
    at android.widget.ImageView.resolveUri(ImageView.java:660)
    at android.widget.ImageView.setImageURI(ImageView.java:393)
    at com.example.user.aitzik4.MainActivity.onCreate(MainActivity.java:114)
    at android.app.Activity.performCreate(Activity.java:5283)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
    at android.app.ActivityThread.access$700(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5279)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)

This is my MainActivity:

https://pastebin.com/x0bDdDLe

The function which recives the images is called handleSendImage().

Thank you!

Kishan Viramgama
  • 893
  • 1
  • 11
  • 23
AmitAM
  • 41
  • 2
  • 8
  • 1
    Post code here rather than links. And have you used the heap profiler to find whats leaking yet? Its probably one of your activities. – Gabe Sechan Sep 14 '18 at 15:42
  • 1
    The code, but not _all_ the code. Preferably something like a [mcve]. – DavidS Sep 14 '18 at 15:44

3 Answers3

0

Looking at the handleSendImage method:

You need to scale down the images to avoid outOfMemory Exceptions. Your images are prolly too large and takes a whole lot of memory.

To scale down your images you can check this.

https://developer.android.com/topic/performance/graphics/load-bitmap#java#load-bitmap

Or simply just use Picasso Library

http://square.github.io/picasso/

Paul Okeke
  • 1,384
  • 1
  • 13
  • 19
0

Using high-res image will cause memory issue if not handled properly.

You can refer some of below posts to handle images in Android:

java.lang.outofmemoryerror bitmap size exceeds vm budget on bitmap

Strange out of memory issue while loading an image to a Bitmap object

android Image view out of memory error

Quick work-around: Set largeHeap="true" in your manifest. But this is not fool-proof.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
0

AndroidManifest.xml

android:largeHeap="true"

 <application
    android:name="com.xxx.xx"
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/icon"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme">

        -----

  </application>
Kishan Viramgama
  • 893
  • 1
  • 11
  • 23
  • You should add a disclaimer that `largeHeap="true"` is a bad idea most of the time, this is only a last resort answer. – Enzokie Sep 15 '18 at 01:56