Droid Dev Stream
Part 109: Accessing the Images/media on a device

To access all the images on your device, you can use a cursor:

Cursor mImageCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null, null, null, null );
     do {
        String s[] = mImageCursor.getColumnNames();
        int column = mImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String filename = mImageCursor.getString(column);
        Bitmap bm = BitmapFactory.decodeFile(filename);
   } while ( mImageCursor.moveToNext());

This grabs all the images on the device, goes to the first image, gets its name via access its column called DATA.

You can then create a Bitmap resource from the filename using BitmapFactory.

Not that, if you’ve just added an image to the filesystem, it won’t show up. Not there and not if you try to access the devices images using the Intent choose way:.

Intent intent = new Intent();
startActivityForResult(Intent.createChooser(intent, “Select Picture”),0);

You must refresh the system’s cache of images in the device. You’ll learn how to do that next.

  1. droiddevelop posted this
Blog comments powered by Disqus