Sample code for conversion between Uri and Path in Android

  • 2021-09-05 00:55:05
  • OfStack

Conversion between Uri and Path in Android

Cause

Call the system photo application. To save the picture after taking the picture, we need to specify an Uri to store the picture path. This involves how to convert file path to Uri. Sometimes we need to get the media Uri of the photo according to the path of the photo, so how to convert it?

Android Uri to Path

There are two kinds of conventional Uri encountered now:

The media file's Uri is content://, indicating that this is 1 database data. Go to the database query and return normally. The 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 brief book, there is an article Android Uri to Path, which introduces how to convert Uri returned from the photo album into Media Uri, and then obtain Path of the picture through the obtained Media Uri. Finally, the corresponding Bitmap object is created through BitmapFractory.

File Uri To Path

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


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

Here GetImageUri. getImageStreamFromExternal is 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;
  }

This static method converts the path of the file in the Pictures directory under the external storage path to File Uri.

Android Path To Uri

File Path To File Uri

Directly on 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 get File Uri to the specified path.

File Path To Media Uri

Directly on 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 of all, we get Uri and mediaUri of photo album database table. We then take advantage of the ContentResolver. query () method, and selectionArgs passes in the filename obtained according to the specified path to get an cursor object. Then we get the ID of the specified file through this cursor object. Finally, the final Media Uri is obtained by combining mediaUri and Id of the picture with ContentUri.

MediaStore

This class is very important. The official document describes: The Media provider contains meta data for all available media on both internal and external storage devices. It probably means that this class contains metadata for all media files stored internally and externally on the device. For example, the system camera can be turned on by specifying Intent with MediaStore.ACTION_IMAGE_CAPTURE as action, MediaStore.EXTRA_OUTPUT as the key to store Uri...

Excuse me... In short, this class is very important when accessing media files.

There are also one column name corresponding to the database table, 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 you need to use it, you can consult the documentation.


Related articles: