Android list all images available

2019-01-25 18:11发布

问题:

I m making an application which requires me to list all the images available on the SD-Card of the phone.

i tried querying the ContentResolver way i.e.

Cursor image = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, new String[]{Images.Media._ID,Images.Media.DATA,Images.Media.DISPLAY_NAME}, null, null, null);

but without any result. Is there any way i can get the list or if thats not possible then is there any possible intent (e.g. PICK) by which i can allow the user to select a file and then access the path of the file the user selected??

Helppppp guys...

回答1:

You could use the gallery activity to select images, something like this:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

in the callback for the activity the file uri will be in the intent parameter



回答2:

//where contextObject is your activity
ContentResolver cr = contextObject.getContentResolver();

String[] columns = new String[] {
                ImageColumns._ID,
                ImageColumns.TITLE,
                ImageColumns.DATA,
                ImageColumns.MIME_TYPE,
                ImageColumns.SIZE };
cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns, null, null, null);

My code is pretty much like yours (except broken down) and it works. You don't need to go about asking the Gallery Intent for stuff, it should work. My two guesses are:

1) Make sure your USB storage is not mounted, if it is you'll see no external images.

2) Maybe a permissions issue? Try adding GLOBAL_SEARCH permission to see if that helps at all.