get External Media by Uris - Android android.os

Android examples for android.os:External Storage

Description

get External Media by Uris

Demo Code

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore;
import java.util.ArrayList;
import java.util.List;

public class Main{

    public static List<Uri> getExternalMediaUris(ContentResolver resolver,
            String type) {/*from  w ww . j av a 2s  .co  m*/
        final String[] columns = { BaseColumns._ID };
        final String selection = MediaColumns.MIME_TYPE + " LIKE ?";
        final String[] selectionArgs = { type.replace('*', '%') };

        Cursor cursor = resolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                selection, selectionArgs, null);

        cursor.moveToFirst();

        ArrayList<Uri> uris = new ArrayList<Uri>();

        for (int i = 0; i < cursor.getCount(); i++) {
            String id = cursor.getString(0);
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                    .buildUpon().appendPath(id).build();
            uris.add(uri);

            cursor.moveToNext();
        }

        cursor.close();

        return uris;
    }

}

Related Tutorials