amanda45
amanda45

Reputation: 595

get images from gallery only

I need to get the uri/paths of all the images taken by the camera (gallery). How can I modify the code below to give me only the images from the gallery. The code below gives me images from other folders too.

public ArrayList<String> getImages()
    {
        ArrayList<String> paths = new ArrayList<String>();
        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media.DATE_ADDED;
        //Stores all the images from the gallery in Cursor
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);
        //Total number of images
        int count = cursor.getCount();

        //Create an array to store path to all the images
        String[] arrPath = new String[count];

        for (int i = 0; i < count; i++) {
            cursor.moveToPosition(i);
            int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            //Store the path of the image
            arrPath[i]= cursor.getString(dataColumnIndex);
            paths.add(arrPath[i]);

        }

        return  paths;
    }

Upvotes: 1

Views: 1198

Answers (1)

VikasGoyal
VikasGoyal

Reputation: 3376

Just provide the selection argument in query

public ArrayList<String> getImages()
    {
        ArrayList<String> paths = new ArrayList<String>();
        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
         String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
        String[] selectionArgs = new String[] {
            "Camera"
        };
        final String orderBy = MediaStore.Images.Media.DATE_ADDED;
        //Stores all the images from the gallery in Cursor
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, orderBy);
        //Total number of images
        int count = cursor.getCount();

        //Create an array to store path to all the images
        String[] arrPath = new String[count];

        for (int i = 0; i < count; i++) {
            cursor.moveToPosition(i);
            int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            //Store the path of the image
            arrPath[i]= cursor.getString(dataColumnIndex);
            paths.add(arrPath[i]);

        }

        return  paths;
    }

Upvotes: 1

Related Questions