0

First, I'm new to developing for Android and this forum. My question is this: I was looking at the "Hello world view: gridview" example and was wondering, how do I load all my images dynamically? For example in the ImageAdapter.java file, at the end there is:

private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

But what if I don't know the images name (as the user will add new images). What I would like to do is grab the image name from an xml file and then load it into an array. How do I accomplish this?

Evan Porter
  • 2,987
  • 3
  • 32
  • 44
jason
  • 3,821
  • 10
  • 63
  • 120
  • 1
    I'm a bit confused. Are you asking how to get image names from an XML file or are you asking how, once you have an array of image names, to get the images? Or are you asking how to put those images in a gridview? – Pace Dec 29 '09 at 19:01
  • sorry for the confusion. i want to put the images into a grid view but because i wont necessarily know what the images names are, i need to add them in a dynamic way. the images will be stored on the phone not downloaded via the internet – jason Dec 29 '09 at 23:26
  • what i would like to do is this: 1. in the drawable folder, i have a group of images. i dont know their names because they will be added by the user at run time 2. i want to load these images into gridview – jason Dec 30 '09 at 18:11
  • Did you figure this out? I'm also wondering the same sort of thing but I don't know java, i'm a php dev. In my case the files are named c1-35.png so in php I could create a for loop and add them but i'm not sure how it works with java and the nested class drawable thing. – jmoz May 11 '10 at 09:05

2 Answers2

0

seems like you are looking to get images from somewhere else other than the static resources. well here's one link which shows how to retrieve images from remote location.

http://www.anddev.org/gallery_with_remote_images-t769.html

Raja
  • 6,354
  • 9
  • 30
  • 34
  • no, the images are a static resource ( the user can save images to a folder and i want the gridview to show all the images from that folder) – jason Dec 29 '09 at 23:27
0

If you want to dynamically show images that are on your device, you should use a Gridview 'backed' by a cursor adaptor (I would suggest a SimpleCursorAdapter).

Instead of hardcoding the image names in an array, your adapter will get the image names from androids mediastore database by creating a 'cursor' that queries the store. Then your gridview can use the adapter by doing a 'setAdapter()' call.

Here is an example of how to display the images in your gallery using a cursoradapter and gridview:

http://android-er.blogspot.co.uk/2012/11/list-mediastoreimagesthumbnails-in.html

Plus, you will want to use the sample code mentioned in @achildress answer in this stackoverflow answer: Displaying images from a specific folder on the SDCard using a gridview

The following code will give you only images in your specific directory:

private Cursor cursor;
private int columnIndex;

First, obtain a cursor of image IDs located in the folder:

Gallery g = (Gallery) findViewById(R.id.gallery);
// request only the image ID to be returned
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection, 
        MediaStore.Images.Media.DATA + " like ? ",
        new String[] {"%myimagesfolder%"},  
        null);
// Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));

Then, in the ImageAdapter for the Gallery, obtain the thumbnail to display:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(context);
    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // obtain the image URI
    Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
    String url = uri.toString();
    // Set the content of the image based on the image URI
    int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
    Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    i.setImageBitmap(b);
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);
    return i;
}
Community
  • 1
  • 1
DEzra
  • 2,978
  • 5
  • 31
  • 50