Simple records between Bitmap File and Uri in Android

  • 2021-11-02 02:22:30
  • OfStack

Introduction:

Feeling Uri, File, bitmap more confusion, here to record, convenient later view. The following words do not say much, to see a detailed introduction

Bitmap, File and Uri

1. Convert a file path path to File


String path ;
File file = new File(path)

2. Talk about converting one Uri into one path

Take selecting a picture as an example:


String path = FileTools.getRealPathFromUri(content,uri);
// Custom methods are shown below 
 public static String getRealPathFromUri(Context context, Uri uri) {

 if (null == uri) return null; // Incoming Uri Empty , End method 

 final String scheme = uri.getScheme(); // Get Uri Adj. scheme

 String realPath = null;

 if (scheme == null)
  realPath = uri.getPath(); // If scheme Empty  
 else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
  realPath = uri.getPath(); // If you get it scheme With file Beginning 
 } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
  // Get scheme With content Beginning 
  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(); // You must close 
  }
 }
 
// After obtaining the real path through the above transformation, , Judge 1 Go down this path , If it is still empty, , Indicates that it is possible that the file exists externally sd On the card , Not built-in sd Card .
 if (TextUtils.isEmpty(realPath)) {
  if (uri != null) {
  
  String uriString = uri.toString();
  int index = uriString.lastIndexOf("/"); // Matching  /  In 1 The last position in the path 

  String imageName = uriString.substring(index);
  // The final result obtained by passing 1 Position , Then truncate the string after this position ,  So you can get the file name  

  File storageDir;

  storageDir = Environment.getExternalStoragePublicDirectory(
   Environment.DIRECTORY_PICTURES); // View files for public photos of external storage cards 

  File file = new File(storageDir, imageName);
  // Create your own file ,

  if (file.exists()) {
   realPath = file.getAbsolutePath();
  } else {
//  // Then it is stored externally sd Application cache of card file Medium 
   storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
   File file1 = new File(storageDir, imageName);
   realPath = file1.getAbsolutePath();
  }
  }
 }
 return realPath;

 

  For example, I am in android 8.0  When running on 
  Select the after the photo Uri : content://media/external/images/media/568344
  After the above method conversion is completed, : /storage/emulated/0/com.appben.appche/browser-photos/1550297407488.jpg
 

 }

3. Conversion of File to path

String path = file.getPath();
Converts this abstract pathname to a 1 pathname string. The resulting string uses the default name separator to separate the names in the name sequence.

String path = file.getAbsolutePath();
If the abstract pathname is already an absolute pathname, the pathname string is returned, as is the case with getPath () method 1. If this abstract pathname is an empty abstract pathname, the pathname string of the current user directory is returned,
The directory is specified by the system attribute user. dir. Otherwise, parse the pathname in a system-specific manner.
On an UNIX system, a 1-relative pathname can be made absolute by parsing it against the current user directory. On the Microsoft Windows system,
A 1 relative pathname is parsed by the current drive directory (if any) specified by the pathname,
Make the pathname an absolute pathname; Otherwise, you can analyze it according to the current user directory.

getCanonicalPath
The canonical pathname is an absolute pathname and is unique. The exact definition of a canonical pathname is system-dependent. If necessary, this method first converts the pathname to an absolute pathname,
This is similar to calling the getAbsolutePath () method and then mapping it to its unique pathname in a system-specific manner.
This usually involves removing redundant names (such as "." and "..") from pathnames, analyzing symbolic connections (for UNIX platforms), and
Convert the drive name to standard case (for the Microsoft Windows platform).
Each pathname representing an existing file or directory has a unique canonical form. Each pathname representing a non-existent file or directory also has a canonical form of 1 unique 1
. The canonical form of a non-existent file or directory pathname may differ from the canonical form of the same 1 pathname after the file or directory is created.
Similarly, the canonical form of an existing file or directory pathname may differ from the canonical form of the same 1 pathname after the file or directory is deleted.

The following is an example mentioned in the reference article


https://blog.csdn.net/qq_39949109/article/details/80609472

File file = new File(".\\test1.txt");
File file = new File("D:\\workspace\\test\\test1.txt");
 System.out.println("----- Default relative path: Get different paths ------");
 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 paths ------
.\test1.txt
D:\workspace\test\.\test1.txt
----- Default absolute path : Get the same path ------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt


 File file = new File("..\\src\\test1.txt");
 System.out.println(file.getAbsolutePath());
 System.out.println(file.getCanonicalPath());
// The result obtained 
D:\workspace\test\..\src\test1.txt
D:\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. Conversion of URI to File


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

6. Conversion of File to URI


URI uri = file.toURI();

7. Conversion of Path to Uri


Uri uri = Uri.parse(path);

8. Uri to Bitmap of the picture


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

9. File goes to bitmap


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

10. Transferring bitmap to file can be understood as saving bitmap.


// Create your own file object for the file you want to save 
BuffferedOutPutStream bos = 
new BufferedOutputStream(new FileOutputStream(file));
bos.flush;
bos.close;

Summarize


Related articles: