仅从图库中获取图像

时间:2017-04-27 03:14:43

标签: android android-intent android-activity android-cursor

我需要获取相机拍摄的所有图像的uri /路径(图库)。如何修改下面的代码,仅向我提供图库中的图像。下面的代码也为我提供了其他文件夹中的图像。

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;
    }

1 个答案:

答案 0 :(得分:1)

只需在查询中提供选择参数

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;
    }