Introduction:
I feel that Uri, File, and bitmap are quite confusing. I will record it here for easy viewing in the future. I won’t say much below, let’s take a look at the detailed introduction together.
Bitmap, File and Uri
1. Convert a file path to File
String path ; File file = new File(path)
2. Convert a Uri to a path
Take choosing a picture as an example:
String path = (content,uri); //The custom method is below public static String getRealPathFromUri(Context context, Uri uri) { if (null == uri) return null; //The Uri passed in is empty, end method final String scheme = (); //Get Uri's scheme String realPath = null; if (scheme == null) realPath = (); //If the scheme is empty else if (ContentResolver.SCHEME_FILE.equals(scheme)) { realPath = (); //If the obtained scheme starts with a file } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { //The obtained scheme starts with content Cursor cursor = ().query(uri, new String[]{}, null, null, null); if (null != cursor) { if (()) { int index = (); if (index > -1) { realPath = (index); } } (); // Must be closed } } //After obtaining the real path through the above conversion, judge this path. If it is still empty, it means that the file may exist on the external SD card, not the built-in SD card. if ((realPath)) { if (uri != null) { String uriString = (); int index = ("/"); //Match / Last location in a path String imageName = (index); //By obtaining the last position and then intercepting the string after this position, so that you can get the file name File storageDir; storageDir = ( Environment.DIRECTORY_PICTURES); //View files of public photos of external memory card File file = new File(storageDir, imageName); //Create it into a file yourself, if (()) { realPath = (); } else { // // Then it is stored in the application cache file of the external SD card storageDir = (Environment.DIRECTORY_PICTURES); File file1 = new File(storageDir, imageName); realPath = (); } } } return realPath; For example, I'm inandroid 8.0 When running After selecting the photoUri : content://media/external/images/media/568344 After the above method is converted: /storage/emulated/0//browser-photos/ }
3. Convert File to path
String path = ();
Convert this abstract pathname to a pathname string. The resulting string uses the default name delimiter to separate names in the name sequence.String path = ();
If this abstract pathname is already an absolute pathname, the pathname string is returned, which is the same as the getPath() method. If this abstract pathname is an empty abstract pathname, the pathname string of the current user directory is returned.
This directory is specified by system properties. Otherwise, analyze this pathname in a system-related manner.
On UNIX systems, by analyzing a relative path name based on the current user directory, the path name can be made an absolute path name. On Microsoft Windows systems,
A relative path name is analyzed by the current drive directory specified by the path name (if any),
This path name can be made an absolute path name; otherwise, it can be analyzed based on the current user directory.getCanonicalPath
The canonical pathname is an absolute pathname and is unique. The accurate definition of the specification pathname is related to the system. If necessary, this method first converts the path name to an absolute path name.
This is the same as calling the getAbsolutePath() method and then map it to its unique pathname in a system-related way.
This usually involves removing unnecessary names from path names (such as "." and ".."), analyzing symbolic connections (for UNIX platforms), and
Convert drive names to standard case (for Microsoft Windows platforms).
Indicates that each pathname of an existing file or directory has a unique canonical form. Each pathname that indicates that a non-existent file or directory also has a unique canonical form.
. The canonical form of non-existent file or directory pathnames may be different from the canonical form of the same pathname after the file or directory is created.
Similarly, the canonical form of an existing file or directory pathname may be different from the canonical form of the same pathname after deleting a file or directory.
Here is an example mentioned in the article
/qq_39949109/article/details/80609472 File file = new File(".\\"); File file = new File("D:\\workspace\\test\\"); ("------Default relative path: get different paths------"); (()); (()); ("-------Default absolute path: get the same path------"); (()); (()); turn out: -----Default relative path:Different paths------ .\ D:\workspace\test\.\ -----Default absolute path:Get the same path------ D:\workspace\test\ D:\workspace\test\ File file = new File("..\\src\\"); (()); (()); //The results obtainedD:\workspace\test\..\src\ D:\workspace\src\
4. The difference between URI and Uri
URI Yes subclass
Uri is a subclass, Uri cannot be instantiated
5. Convert URI to File
File file = null; try{ file = new File(new URI(())); }catch(URISyntaxException e){ (); }
6. Convert File to URI
URI uri = ();
7. Convert Path to Uri
Uri uri = (path);
8. Uri to Bitmap of pictures
Bitmap bitmap = ((uri))
9. Go to bitmap
Bitmap bitmap = (); //thisfileIf it was created by the real pathfile
10. To convert bitmap to file, it can be understood as saving bitmap.
//Create the file object of the file you want to saveBuffferedOutPutStream bos = new BufferedOutputStream(new FileOutputStream(file)); ; ;
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.