how to convert `content://media/external/images/media/Y` to `file:///storage/sdcard0/Pictures/X.jpg` in android?

后端 未结 2 1801
我寻月下人不归
我寻月下人不归 2020-12-04 21:35

I\'m trying to upload image to Google Drive from my android app,

based on this tutorial.

When I debug their sample project I see a typical fileUri =

2条回答
  •  醉梦人生
    2020-12-04 22:19

    Will something like this work for you? What this does is query the content resolver to find the file path data that is stored for that content entry

    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    

    This will end up giving you an absolute file path that you can construct a file uri from

提交回复
热议问题