1

I am creating an app which loads images in a GridView but depending on the number of images I place in my array I get the java.lang.outofmemory error and the app crashes.
I have an ImageAdapter where the loading of the images occurs, I was following this tutorial from Androidhive . My images in total are about 4.53MB with the largest one being 1.1MB. Some images are fairly smaller. Sometimes when I load the images from my code one at a time the app runs,but when I do all images at once it crashes.

This answer seemed sensible but I could not follow with the way my project is setup. I just have a GridView and get the ImageView programatically in my code but they seem to reference from xml file.
Here is my getView() method:
@Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mThumbsIds[position]); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); return imageView; } where the mThumbsId is the integer array that is storing my image references from @drawable folder. I need help resizing the images with the way my project is set up and maybe the other answer can help but I cannot follow their structure.
My xml file with the gridview as well:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="150dp"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:soundEffectsEnabled="true"
android:stretchMode="columnWidth" > 

</GridView>


Thanx in advanced

2
  • The size in Megabytes does not matter that much. How big is the size of the images in terms of resolution? Commented Nov 2, 2014 at 15:13
  • largest one 1161*1161 and smallest one is 350*350 I just got them off the internet. Should I resize? @PhilippJahoda Commented Nov 2, 2014 at 15:21

1 Answer 1

1

The image of 1161x1161 pixels you mentioned is internally converted into a Bitmap. This Bitmap (if 32Bit) will occupy approximately 5MB (1161 x 1161 x 4 bytes) of heap memory.

Since you will have multiple of these images in memory when used in a GridView, it is no surprise you run out of memory. So yes, you should resize your images before loading them into the GridView.

Read this question/answer on how to resize: High resolution Image - OutOfMemoryError

Sign up to request clarification or add additional context in comments.

1 Comment

using your solution and setting the imageview using the imageView.setImageBitmap(decodeSampledBitmapFromResource()) method worked. Thanx @PhilippJahoda

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.