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

后端 未结 2 1793
我寻月下人不归
我寻月下人不归 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:08

    If you just want the bitmap, This too works

    InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
    if( inputStream != null ) inputStream.close();
    

    sample uri : content://media/external/images/media/12345

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题