Interconversion between Uri and Path in Android

Android Uri to Path

There are two kinds of regular Uri encountered now:

  • The Uri of the media file is content://, indicating that this is a database data. Going to the database query returns normally.
  • Other file Uri is file://, indicating that this is a file. This uri is generated by the Uri.fromFile(File file) method.

Media Uri To Path

In my short book, there is an article Android Uri to Path which introduces how to convert the Uri returned from the album to Media Uri, and then obtain the Path of the picture through the obtained Media Uri. Finally, the corresponding Bitmap object is created through BitmapFractory.

File Uri To Path

This conversion is relatively simple. We can directly use the Uri.getPath() method provided by the Android SDK to obtain the corresponding path, and then use Java IO to obtain the input stream and create a Bitmap. If we want to get the input stream directly through File Uri, we can get the input stream by calling ContentResolves.openInputStream(Uri uri).

bitmap = BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(
                            GetImageUri.getImageStreamFromExternal("Screenshots/Screenshot.png"))
            );

Here GetImageUri.getImageStreamFromExternalis a tool class I wrote myself:


public static Uri getImageStreamFromExternal(String imageName) {
        File externalPubPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
        );

        File picPath = new File(externalPubPath, imageName);
        Uri uri = null;
        if(picPath.exists()) {
             uri = Uri.fromFile(picPath);
        }

        return uri;
    }

Through this static method, the path of the file in the Pictures directory under the external storage path can be converted into a File Uri.

Android Path To Uri

File Path To File Uri

Go directly to the code:

public static Uri getImageStreamFromExternal(String imageName) {
        File externalPubPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
        );

        File picPath = new File(externalPubPath, imageName);
        Uri uri = null;
        if(picPath.exists()) {
             uri = Uri.fromFile(picPath);
        }

        return uri;
    }

Here we see that the core part is to use the Uri.fromFile() method to obtain the File Uri of the specified path.

File Path To Media Uri

Go directly to the code:

public static Uri getMediaUriFromPath(Context context, String path) {
        Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = context.getContentResolver().query(mediaUri,
                null,
                MediaStore.Images.Media.DISPLAY_NAME + "= ?",
                new String[] {path.substring(path.lastIndexOf("/") + 1)},
                null);

        Uri uri = null;
        if(cursor.moveToFirst()) {
            uri = ContentUris.withAppendedId(mediaUri,
                    cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
        }
        cursor.close();
        return uri;
    }

Code analysis: First, we get the Uri, mediaUri of the album database table. Then we use the ContentResolver.query() method, and the selectionArgs pass in the filename obtained from the specified path to get a cursor object. Then through this cursor object we get the ID of the specified file. Finally, combine the mediaUri and the Id of the image through ContentUri to get the final Media Uri.

Media Store

This class is very important. The official documentation introduces: The Media provider contains meta data for all available media on both internal and external storage devices. It means that this class contains metadata for all media files stored in the device's internal storage and external storage. For example, the system camera can be opened by specifying MediaStore.ACTION_IMAGE_CAPTURE as the action's Intent, and MediaStore.EXTRA_OUTPUT is the key to specify the storage Uri...

Slag slag English translation, sorry... In short, this class is very important when accessing media files.

There are also some corresponding database table column names, DATA in android.provider.MediaStore.MediaColumns refers to the file path, DISPLAY_NAME represents the file name... and _ID in android.provider.BaseColumns is the ID of the media file. When necessary, you can consult the documentation.

refer to

Conversion path for Android File Uri

[Android] URI those things



Author: FoolishDev
Link: https://www.jianshu.com/p/33bc363290e9
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772826&siteId=291194637