I am new in android and I want to read all the files from external storage in order to make a file explorer app but I can't find a way to access those files. What should I use instead of environment.getexternalstoragedirectory()?
Use getExternalFilesDir()
, getExternalCacheDir()
, getExternalMediaDir
() ... Read more .
For example ( getExternalFilesDir
) :
public static File getAppFileDir(Context context) {
File file;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
file = context.getExternalFilesDir(null);
} else {
file = context.getFilesDir();
}
return file;
}
will give you:
android/data/data/your_package_name/files
If you still want to use getExternalStorageDirectory
:
public static File getExternalStorageDirectory(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
//noinspection deprecation
return Environment.getExternalStorageDirectory();
} else {
try {
File file = getAppFileDir(context);
if (null != file) {
//noinspection ConstantConditions
return file.getParentFile().getParentFile().getParentFile().getParentFile();
} else {
return new File("");
}
} catch (Throwable e) {
e.printStackTrace();
return new File("");
}
}
}
But don't use it in a release app, cause I'm not sure if it will work on all devices.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.