<ImageView
android:id="@+id/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/bg" />
@drawable/bg is 1600 * 1800 (W * H)
I tried using
BitmapFactory.decodeResource(getResources(), R.drawable.bg, options);
using inSampleSize
Here are the logs:
02-05 05:32:05.841: E/screenWidth in px =(876): 768
02-05 05:32:05.841: E/screenheight in px =(876): 1184
02-05 05:32:05.841: E/screenDensity =(876): 2.0
02-05 05:32:05.841: E/screenDensityDpi =(876): 320
02-05 05:32:05.841: E/decodeSampledBitmapFromResource reqWidth =(876): 768
02-05 05:32:05.851: E/decodeSampledBitmapFromResource reqHeight =(876): 1184
02-05 05:32:05.921: E/decodeSampledBitmapFromResource imageHeight =(876): 1800
02-05 05:32:05.921: E/decodeSampledBitmapFromResource imageWidth =(876): 1600
02-05 05:32:05.921: E/decodeSampledBitmapFromResource imageType =(876): image/jpeg
02-05 05:32:05.951: E/calculateInSampleSize: inSampleSize =(876): 1
02-05 05:32:09.291: E/dalvikvm-heap(876): Out of memory on a 44441616-byte allocation.
02-05 05:32:09.391: E/AndroidRuntime(876): FATAL EXCEPTION: main
02-05 05:32:09.391: E/AndroidRuntime(876): Process: com.example.splash, PID: 876
02-05 05:32:09.391: E/AndroidRuntime(876): java.lang.OutOfMemoryError
02-05 05:32:09.391: E/AndroidRuntime(876): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:445)
02-05 05:32:09.391: E/AndroidRuntime(876): at com.example.splash.SecondActivity.decodeSampledBitmapFromResource(SecondActivity.java:103)
02-05 05:32:09.391: E/AndroidRuntime(876): at com.example.splash.SecondActivity.onCreate(SecondActivity.java:57)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.Activity.performCreate(Activity.java:5231)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.os.Handler.dispatchMessage(Handler.java:102)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.os.Looper.loop(Looper.java:136)
02-05 05:32:09.391: E/AndroidRuntime(876): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-05 05:32:09.391: E/AndroidRuntime(876): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 05:32:09.391: E/AndroidRuntime(876): at java.lang.reflect.Method.invoke(Method.java:515)
02-05 05:32:09.391: E/AndroidRuntime(876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-05 05:32:09.391: E/AndroidRuntime(876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-05 05:32:09.391: E/AndroidRuntime(876): at dalvik.system.NativeStart.main(Native Method)
The thing is, for a device which has height 1184 the
inSampleSize will be 1 (Hence no change)
So, its kind of useless going through all this process and the same OOM occurs.
This single screen shows that it takes 44 mb of memory, which I got using:
long totalMem = Runtime.getRuntime().totalMemory();
If I set image src as @null, the above value is 3 mb
Is this a reliable way to find the memory usage by the app?
Please help me how to load such high resolution images without any OOM.
Picasso.with(context).load(R.drawable.bg).into(iv);
USe picaso image loader for that ots very efficent link
Picasso and Universal-image-loader are the libraries i have used to load large images.
Picasso: http://square.github.io/picasso/
Universal-image-loader: https://github.com/nostra13/Android-Universal-Image-Loader
Also putting android:largeHeap="true" in your Manifest will grant you extra memory for heavy processes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With