Android Obtaining SDcard Directory and Creating Folder Method
- 2021-10-11 19:34:13
- OfStack
Get the sdcard directory
public static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// Judge sd Does the card exist
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// Get the heel directory
}
return sdDir.toString();
}
Create directories, not limited to directory levels
public static String mkdirs(String path) {
String sdcard = getSDPath();
if (path.indexOf(getSDPath()) == -1) {
path = sdcard + (path.indexOf("/") == 0 ? "" : "/") + path;
}
File destDir = new File(path);
if (!destDir.exists()) {
path = makedir(path);
if (path == null) {
return null;
}
}
return path;
}
private static String makedir(String path) {
String sdPath = getSDPath();
String[] dirs = path.replace(sdPath, "").split("/");
StringBuffer filePath = new StringBuffer(sdPath);
for (String dir : dirs) {
if (!"".equals(dir) && !dir.equals(sdPath)) {
filePath.append("/").append(dir);
File destDir = new File(filePath.toString());
if (!destDir.exists()) {
boolean b = destDir.mkdirs();
if (!b) {
return null;
}
}
}
}
return filePath.toString();
}
Required permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- In sdcard Create in / Permissions to delete files -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
Show local pictures
ImageView view5 = findView(R.id.imageview2);
view5.setImageBitmap(ImageUtils.getLoacalBitmap("/storage/sdcard1/myimage/20160807.jpg"));
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
// The default picture should be displayed here, such as the picture cannot be displayed; Select from the application resource picture
return null;
}
}