Talking about several ways and matters needing attention for android to obtain the path of storage directory of

  • 2021-10-11 19:36:14
  • OfStack

Usually, when we create files/directories, or store pictures, we all need to get the storage path of the mobile phone. Now let's look at several ways to get the storage path of the mobile phone (just call it as a tool class method):

Type 1: Get/storage/emulated/0


public static boolean sdCardIsAvailable() {
 // First, determine whether external storage is available 
 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  File sd = new File(Environment.getExternalStorageDirectory().getPath());
  Log.e("qq", "sd = " + sd);//sd = /storage/emulated/0
  return sd.canWrite();
 } else {
  return false;
 }

Type 2: Get/storage/emulated/0 (similar to above)


public String getSDPath() {
 File sdDir = null;
  // Judge sd Does the card exist 
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); 
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// Get the root directory 
Log.e("qq", " External storage available ..." + sdDir.toString());
}
return sdDir.toString();
}
// Use 
private void saveInRoot() {
 if (getSDPath()!=null) {
  //String fileName = getSDPath() + "/";
  //File file = new File(fileName,"123111");// Parameter 
  //if (!file.mkdir()) {
   //Toast.makeText(ExternalStoreActivity.this, " Directory already exists ...", Toast.LENGTH_SHORT).show();
  //} else {
   //Toast.makeText(ExternalStoreActivity.this, " Create a new directory ...", Toast.LENGTH_SHORT).show();
  //}
 }
}

The above two paths are actually the root directory of the system. For example, my mobile phone is Huawei Glory 6, which is local- > Internal storage under this path

Type 3: Get the designated directory of mobile phone

Note: I suspect that you must know if this directory exists for the phone before you can get it, because there is no guarantee that every phone comes with the same directory

Therefore, it is best to check whether the mobile phone has this path before using it


public File getAlbumStorageDir(String fileName) {
 // Get the directory for the user's public pictures directory.
 // In  Environment.DIRECTORY_DOWNLOADS  Directory to create a file named fileName Folder of   Refresh View 
 File file =
  new File(Environment.getExternalStoragePublicDirectory(Environment.
   DIRECTORY_DOWNLOADS), fileName);// Parameter 2 Is the file name 
 //mkdirs() You can create multi-level directories 
 //mkdir() You can only create 1 Level directory 
 if (!file.mkdir()) {
  Log.e(LOG_TAG, "Directory not created");
  Toast.makeText(ExternalStoreActivity.this, " Directory already exists ...", Toast.LENGTH_SHORT).show();
 } else {
  Toast.makeText(ExternalStoreActivity.this, " Create a new directory ...", Toast.LENGTH_SHORT).show();
 }
 return file;
}

Finally, it should be noted that if the final path we get is of type String, there is now a requirement to store a picture of xx. png under the aaaa path

Achieve:

The storage path should be: aaaa + "/" + xx. png. If "/" is not added, the final image is: aaaaxx. png

Of course, if you don't want to add "/", it is also very simple, just make sure your path is File type


Related articles: