Android gets the absolute path of the picture according to the picture Uri

Keywords: SDK Android

This article is reprinted from a brief book tianma , at: http://www.jianshu.com/p/b168cbe50066

When we need to select and take pictures to show them, we usually do the following:

// Enter the picture selection interface
private void selectImage(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, CHOOSE_IMAGE);
}
// Data acquisition in onActivityResult() callback method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // ... make some judgements
        Uri uri = data.getData();
        // ... let's take a picture
}

As can be seen above, we get Uri type data. To display a picture, you can first get the location of the picture according to the Uri, and then display it on the interface. How to get the absolute path of the picture according to the Uri? This involves the adaptation of Android version. Because the absolute path to obtain the image corresponding to the Uri is different with different Android Api versions, different APIs need to be adapted:

    /**
     * Get the absolute path of the picture according to the Uri of the picture (multiple APIs have been adapted)
     * @return If the image corresponding to Uri exists, the absolute path of the image will be returned, otherwise null will be returned
     */
    public static String getRealPathFromUri(Context context, Uri uri) {
        int sdkVersion = Build.VERSION.SDK_INT;
        if (sdkVersion < 11) {
            // SDK < Api11
            return getRealPathFromUri_BelowApi11(context, uri);
        }
        if (sdkVersion < 19) {
            // SDK > 11 && SDK < 19
            return getRealPathFromUri_Api11To18(context, uri);
        }
        // SDK > 19
        return getRealPathFromUri_AboveApi19(context, uri);
    }

    /**
     * The absolute path of the image is obtained according to the uri according to the adaptation of API 19 or above
     */
    private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) {
        String filePath = null;
        String wholeID = DocumentsContract.getDocumentId(uri);

        // Use ':' to split
        String id = wholeID.split(":")[1];

        String[] projection = { MediaStore.Images.Media.DATA };
        String selection = MediaStore.Images.Media._ID + "=?";
        String[] selectionArgs = { id };

        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                selection, selectionArgs, null);
        int columnIndex = cursor.getColumnIndex(projection[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
        return filePath;
    }

    /**
     * Fit api11-api18, and get the absolute path of the picture according to the uri
     */
    private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
        String filePath = null;
        String[] projection = { MediaStore.Images.Media.DATA };

        CursorLoader loader = new CursorLoader(context, uri, projection, null,
                null, null);
        Cursor cursor = loader.loadInBackground();

        if (cursor != null) {
            cursor.moveToFirst();
            filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
            cursor.close();
        }
        return filePath;
    }

    /**
     * Adapt to api11 or below (excluding api11), and obtain the absolute path of the picture according to the uri
     */
    private static String getRealPathFromUri_BelowApi11(Context context, Uri uri) {
        String filePath = null;
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(uri, projection,
                null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
            cursor.close();
        }
        return filePath;
    }

Then we can get the absolute path of the corresponding image of Uri, and then we can do what we want to do happily~

Reference link:
Android | Display Selected Image and Its Real Path
android get real path by Uri.getPath()

Posted by gnetcon on Sun, 05 Apr 2020 00:45:01 -0700