仅获取相机图像路径的功能,而不是存储在Android手机上的所有图像



我有一个函数可以返回手机上所有图像的路径,但是我只希望它返回相机拍摄的图像。这是函数:

public String[] getPath(){
    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    //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);
        Log.i("PATH", arrPath[i]);
    }
    cursor.close();
    return arrPath;
}

我需要更改什么才能仅获取存储在/DCIM/CAMERA 中的路径?

通常每个Android设备都会将相机图像保存到DCIM目录。这是一种获取保存在该目录中的所有图像的方法。

public static List<String> getCameraImages(Context context) {
    public final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString()+ "/DCIM/Camera";
    public final String CAMERA_IMAGE_BUCKET_ID = String.valueOf(CAMERA_IMAGE_BUCKET_NAME.toLowerCase().hashCode());
    final String[] projection = { MediaStore.Images.Media.DATA };
    final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
    final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
    final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
        projection, 
        selection, 
        selectionArgs, 
        null);
    ArrayList<String> result = new ArrayList<String>(cursor.getCount());
    if (cursor.moveToFirst()) {
        final int dataColumn = 
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        do {
            final String data = cursor.getString(dataColumn);
            result.add(data);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return result;
}

最新更新