Android: Using media files for grid view tutorial causes it to run out of memory after 9 pics?
I'm trying to learn how to display all the images stored on an android phone in a grid view (my goal is to have a user select which image they want to process).
I am adapting the tutorial at:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
to do so.
http://pastie.org/2504664
Above is a link to my modified ImageAdapter class (the only thing I believe needs to be changed).
In it, rather than having an integer array of Resources ids, I have a string array of media file locations, and turn them into a bitmap like so:
imageView.setImageBitmap(BitmapFactory.decodeFile(picture_paths[position]));
I'm getting the file locations in the first place by:
public void populateArray(){
Log.v("Jenny","Populating array");
picture_paths = new String[getCount()];
cursor.moveToFirst();
int count = 0;
while(!cursor.isLast() ){
count ++;
picture_paths[cursor.getPosition()] = cursor.getString(0);
cursor.moveToNext();
}
}
It works like a gem, as long as I have less than 9 pictures (in three columns of three).
Any more than that, and the thing crashes with an "Out of Memory" error. If I just follow the tutorial and use resource images, I can have as many as I like, it seems.
So: Am I going about this the wrong way? Is loading it as a Bitmap to intensive? What alternatives are there?
Displaying the images stored on the phone seems like a really trivial, basic task. What is the "right" way to do this, and is there anyway I can make this way work?
Edit: Testing on my second phone (a Galaxy) makes things work just fine (can display any number of images, though it seems like sometimes the file path retrieved by the cursor are 'null'). On my original testing phone (a myTouch) I ge开发者_高级运维t the memory error no matter how I attempt to display the images (drawable, bitmaps, whatever). Why would I get different behaviors on the two phones?
Edit: Well, one thing that might be going on is that my myTouch has more pictures on it? I took a few more pics with my Galaxy and now it is getting out of memory errors as well.... Is grid view not for displaying photos (if not, what good is it?)
Still limited to 9 on myTouch but can go to 11 on Galaxy
Both crash with an out of Memory error when rotated (i.e the view redraws). (though not with a small amount of pics (less than 6 maybe?)
Try resizing your images to maybe half their size.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 5;
Bitmap previewBitmap = Bitmap.createScaledBitmap(
BitmapFactory.decodeFile(picLocation, o),
85,
85,
false);
imageView.setImageBitmap(previewBitmap);
This should reduce the number of size of the image by 1/5 and 1/25 the number of pixels according to the documentation. Raise or lower o.inSampleSize
to other values depending on what you want. Also, in the createScaledBitmap
you can change the size of the bitmap by changing the 85
to whatever dimensions you want.
Other resources that may help if this doesn't:
android - out of memory exception when creating bitmap
Strange out of memory issue while loading an image to a Bitmap object
Out of memory exception due to large bitmap size
精彩评论