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.
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 get 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?)