I wanted to create an activity in which the user can set the background.
Unfortunately the images are ‘too big’, when I’m loading about 7 images
it throws an exception (Failed to allocate a 74649612 byte allocation with 7804240 free bytes and 7MB until OOM
).
Is there a way to make the images ‘smaller’ inside Android Studio, without scaling it down in photoshop?
@Override
protected void onCreate(Bundle savedInstanceState) {
(...)
ImageView image_1 = (ImageView) findViewById(R.id.image_1);
ImageView image_2 = (ImageView) findViewById(R.id.image_2);
...
image_1.setImageDrawable(getDrawable(R.drawable.background_1));
image_2.setImageDrawable(getDrawable(R.drawable.background_2));
...
I alredy enabled android:largeHeap="true"
in the AndroidManifest.xml.
Thanks for your help.
3
Answers
I thinks so this can help u.
Handling Bitmaps
Or u can use Glide Library.
You can solve this problem by decreasing the size of the drawable images, to do that you need to first convert images to bitmap then use the following code to decrease the size of the bitmap
You can use Glide for loading images and scale them down. You can add this in your Gradle file:
And this could be an approach of how you can use it:
where
images
is anArrayList<String>
containing urls of images (local or remote),thumbnailHeight
is the height of the view containing this image (I also used the height value for width to make it squared),IMAGE_SCALE_FACTOR
is a constant indicating how much your image is going to be scaled down (if you want it smaller, just use values between 0.1 and 0.9)