gosr
gosr

Reputation: 4708

Getting ID of bitmap on SD card

My app has a section where some images are displayed in a gridview.

Before the gridview is set up, the following is done:

When setting up the gridview I do it by using the drawable IDs, which are easy to get as they are in a resource folder ("drawable").

But if I need to include the SD card images as well in the gridview, how can I retrive their IDs?

Upvotes: 1

Views: 1181

Answers (2)

0xC0DED00D
0xC0DED00D

Reputation: 20348

I was having a code to get images from sdcard and convert them into an array of URIs, the ids are like URIs, use them.

    private void getData() {
    File f = new File("/mnt/sdcard/");  
    File[] imagelist = f.listFiles(new FilenameFilter(){  

    public boolean accept(File dir, String name)  
    {  
        return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
    }  
    });  
    mFiles = new String[imagelist.length];  

    for(int i= 0 ; i< imagelist.length; i++)  
    {  
        mFiles[i] = imagelist[i].getAbsolutePath();  
    }  
    mUrls = new Uri[mFiles.length];  

    for(int i=0; i < mFiles.length; i++)  
    {  
        mUrls[i] = Uri.parse(mFiles[i]);             
    }
}

You can use the uri like this:-

    ImageView imageView=new ImageView(this);
    imageView.setImageURI(mUrls[i]);

Upvotes: 2

Squonk
Squonk

Reputation: 48871

Files anywhere other than those packaged with your app (downloaded to the internal or external storage, for instance) don't have resource ids.

Resource ids are a shorthand way of referencing the packaged resources such as strings, images, whatever. These ids are created at build-time and only have meaning for internal resources.

In order to use images obtained from an external source, you access them by filename and, I presume, you'll then create ImageViews which are added to your GridView.

If you need to discern between the different images of the grid (perhaps for touch/click purposes) then it's the ImageView that you deal with rather than being concerned with resource ids or filenames of the images they contain.

Upvotes: 3

Related Questions