Conversion between Uri and Path in Android
reason
Call the system photo application and save the picture after taking the photo, then we need to specify a Uri that stores the image path. This involves how to convert file path to Uri. Sometimes we also need to get the media Uri of the photo based on the path of the photo, so how should we convert it?
Android Uri to Path
There are two types of regular Uri we encounter now:
- The Uri of the media file is content://, which means that this is a database data. Go to the database and return normally.
- The other files Uri are file://, which means that this is a file. This uri is generated by the (File file) method.
Media Uri To Path
There is an article in my JianshuAndroid Uri to PathIt 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 () 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 you want to get the input stream directly through File Uri, we can get the input stream by calling (Uri uri).
bitmap = ( getContentResolver().openInputStream( ("Screenshots/")) );
Here is a tool class I wrote myself:
public static Uri getImageStreamFromExternal(String imageName) { File externalPubPath = ( Environment.DIRECTORY_PICTURES ); File picPath = new File(externalPubPath, imageName); Uri uri = null; if(()) { uri = (picPath); } return uri; }
This static method can convert 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 upload the code:
public static Uri getImageStreamFromExternal(String imageName) { File externalPubPath = ( Environment.DIRECTORY_PICTURES ); File picPath = new File(externalPubPath, imageName); Uri uri = null; if(()) { uri = (picPath); } return uri; }
Here we see that the most core part is to use the () method to obtain the File Uri of the specified path.
File Path To Media Uri
Directly upload the code:
public static Uri getMediaUriFromPath(Context context, String path) { Uri mediaUri = .EXTERNAL_CONTENT_URI; Cursor cursor = ().query(mediaUri, null, .DISPLAY_NAME + "= ?", new String[] {(("/") + 1)}, null); Uri uri = null; if(()) { uri = (mediaUri, ((._ID))); } (); return uri; }
Code analysis: First we get the Uri and mediaUri of the album database table. Then we use the() method and pass the selectionArgs file name obtained according to the specified path to get a cursor object. Then through this cursor object we get the ID of the specified file. Finally, the final Media Uri is combined with the Id of the picture by ContentUri to get the final Media Uri.
MediaStore
This class is very important, the official documentation introduction: 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, by specifying MediaStore.ACTION_IMAGE_CAPTURE as an Intent to open the system camera. MediaStore.EXTRA_OUTPUT is the key to specify the storage of Uri...
Zhazha English translation, sorry... In short, this type is very important when accessing media files.
There are also some corresponding database table column names, DATA refers to the file path, DISPLAY_NAME represents the file name...and _ID is the ID of the media file. When needed, you can check the document.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.