Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis between Bitmap, File and Uri in Android

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis between Bitmap, File and Uri in Android, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Brief introduction:

Bitmap, File and Uri

1. Convert a file path path to File

String path; File file = new File (path)

2. Convert a Uri into a path

Take selecting a picture as an example:

String path = FileTools.getRealPathFromUri (content,uri); / / Custom method public static String getRealPathFromUri (Context context, Uri uri) {if (null = = uri) return null; / / the Uri passed in is empty, and the ending method final String scheme = uri.getScheme (); / / get scheme String realPath = null; if (scheme = = null) realPath = uri.getPath () of Uri; / / if scheme is empty else if (ContentResolver.SCHEME_FILE.equals (scheme)) {realPath = uri.getPath () / / if the resulting scheme begins with file} else if (ContentResolver.SCHEME_CONTENT.equals (scheme)) {/ / the resulting scheme begins with content Cursor cursor = context.getContentResolver (). Query (uri, new String [] {MediaStore.Images.ImageColumns.DATA}, null, null, null); if (null! = cursor) {if (cursor.moveToFirst ()) {int index = cursor.getColumnIndex (MediaStore.Images.ImageColumns.DATA) If (index >-1) {realPath = cursor.getString (index);}} cursor.close (); / / must be closed}} / / after the above conversion to get the real path, determine the path, if it is still empty, it is possible that the file exists on the external SD card, not the internal SD card. If (TextUtils.isEmpty (realPath)) {if (uri! = null) {String uriString = uri.toString (); int index = uriString.lastIndexOf ("/"); / / match / the last location in a path String imageName = uriString.substring (index); / / through the last location obtained, and then intercept the string after that location, so you can get the file name File storageDir StorageDir = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES); / / View the file File file = new File (storageDir, imageName) of the public photo of the external storage card; / / create a file yourself, if (file.exists ()) {realPath = file.getAbsolutePath ();} else {/ then storageDir = context.getExternalFilesDir (Environment.DIRECTORY_PICTURES) in the application cache of the external SD card; File file1 = new File (storageDir, imageName) RealPath = file1.getAbsolutePath ();}} return realPath; for example, when I run on android 8.0, I select Uri: content://media/external/images/media/568344 after the photo is converted by the above method: / storage/emulated/0/com.appben.appche/browser-photos/1550297407488.jpg}

3. Convert File to path

String path = file.getPath ()

Converts this abstract pathname to a pathname string. The resulting string uses the default name delimiter to separate the names in the name sequence.

String path = file.getAbsolutePath ()

If this abstract pathname is already an absolute pathname, the pathname string is returned, just like 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 the system property user.dir. Otherwise, the pathname is parsed in a system-related manner.

On UNIX systems, by analyzing a relative pathname according to the current user directory, the pathname can be made absolute. On the Microsoft Windows system

Parses a relative pathname by the current drive directory (if any) specified by the pathname

You can make the pathname an absolute pathname; otherwise, you can analyze it based on the current user directory.

GetCanonicalPath

The canonical pathname is absolute and unique. The exact definition of the canonical path name is related to the system. If necessary, this method first converts the pathname to an absolute pathname

This is the same as calling the getAbsolutePath () method and then mapping it to its unique path name in a system-related manner.

This usually involves removing redundant names from the path name (such as "." And ".."), analyze symbolic links (for UNIX platforms), and

Convert the drive name to standard case (for Microsoft Windows platforms).

Each pathname that represents an existing file or directory has a unique canonical form. Each pathname that represents a non-existent file or directory also has a unique canonical form

. The canonical form of a non-existing file or directory pathname 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 the file or directory is deleted.

Here is an example mentioned in the article

Https://blog.csdn.net/qq_39949109/article/details/80609472File file = new File (".\\ test1.txt"); File file = new File ("D:\\ workspace\\ test\\ test1.txt"); System.out.println ("- default relative path: get a different path -"); System.out.println (file1.getPath ()); System.out.println (file1.getAbsolutePath ()) System.out.println ("- default absolute path: get the same path -"); System.out.println (file2.getPath ()); System.out.println (file2.getAbsolutePath ()) The result is:-default relative path: get different path -.\ test1.txtD:\ workspace\ test\.\ test1.txt- default absolute path: get the same path-D:\ workspace\ test\ test1.txtD:\ workspace\ test\ test1.txt File file = new File ("..\ src\\ test1.txt"); System.out.println (file.getAbsolutePath ()) System.out.println (file.getCanonicalPath ()); / / the result D:\ workspace\ test\..\ src\ test1.txtD:\ workspace\ src\ test1.txt

4. The difference between URI and Uri.

URI is a subclass of java.net

Uri is a subclass of android.net. Uri cannot be instantiated.

5. Convert URI to File

File file = null;try {file = new File (new URI (uri.toString ());} catch (URISyntaxException e) {e.printStackTrace ();}

6. Convert File to URI

URI uri = file.toURI ()

7. Convert Path to Uri

Uri uri = Uri.parse (path)

8. The Uri of the picture is converted to Bitmap

Bitmap bitmap = BitmapFactory.decodeStream (contentResolver.openInputStream (uri))

9. File is transferred to bitmap

Bitmap bitmap = BitmapFactory.decodeFile (file.getPath); / / this file is the file created by the real path

10. The conversion from bitmap to file can be understood as saving bitmap.

/ / create the file object BuffferedOutPutStream bos = new BufferedOutputStream (new FileOutputStream (file)) of the file you want to save; bos.flush;bos.close; thanks you for reading this article carefully. I hope the article "sample Analysis between Bitmap, File and Uri in Android" shared by the editor will be helpful to you. At the same time, I hope you will support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report