Android 13 mobile phone picture storage File path to Uri, Java

Android 13 mobile phone picture storage File path to Uri, Java

    public static Uri getImageUri(Context context, String filePath) {
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
                new String[]{filePath}, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        }
    }

Android converts it into a Uri according to the drawable id of the image resource, java_zhangphil's blog-CSDN blog android converts it into a Uri tool method according to the drawable id of the image resource. https://blog.csdn.net/zhangphil/article/details/129431755

Guess you like

Origin blog.csdn.net/zhangphil/article/details/129640812