java.lang.OutOfMemoryError: Failed to allocate a 313194252 byte allocation with 11901284 free bytes and 174MB until OOM
I got this at this line:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.handskizze);
My Picture has a size of 2480*3508 at 300dpi.
Is it maybe to big? How can I make it smaller?
-
Use universal image loader to load too large imageAnand Savjani– Anand Savjani2015年06月18日 09:32:23 +00:00Commented Jun 18, 2015 at 9:32
3 Answers 3
Whenever there is a requirement to load any image always try to use any ImageLoader library.
There are plenty of options available.
Here are some of the best libraries that developers uses. Try anyone of them which you fill easy to implement.
- Universal Image Loader: https://github.com/nostra13/Android-Universal-Image-Loader
- Picasso: http://square.github.io/picasso/
- Glide: https://github.com/bumptech/glide
You can find some more libraries but I feel these 3 are used most.
2 Comments
In manifest use below attribute with application tag
android:largeHeap="true"
Also use
BitmapFactory.Options o=new BitmapFactory.Options();
o.inSampleSize = 2; //change value from 0 to 8 as per need and image resolution
myBitMap=BitmapFactory.decodeResource(getResources(),R.drawable.handskizze, o);
Comments
First of all, i would say your image is way too big for mobile devices. You should scale it down using Photoshop (or any other image editing software) to reduce its size. By doing so you minimize the size of your .apk and the processing time it takes to downscale the image.
Additionally, i would recomment you to use an image loader library. Im using the Universal Image Loader in most of my projects. It gets the measurement of the ImageView you want to load your image into, and downscales the image to reduce its size. You should give it a try ;)