[英]Test programmatically if in Android 10+ the legacy external storage access is disabled
Is there a simple API call that would tell me if an app running on Android 10 or later must use Scoped Storage access, that is, if the normal file access, aka "Legacy External Storage" is disabled?是否有一个简单的 API 调用可以告诉我在 Android 10 或更高版本上运行的应用程序是否必须使用范围存储访问,也就是说,如果正常文件访问,即“传统外部存储”被禁用? Note that even if the app in AndroidManifest.xml, section declared:
请注意,即使应用程序在 AndroidManifest.xml 部分声明:
android:requestLegacyExternalStorage="true"
the file access may still be denied due to Google policy changes in the future, so testing this value is useless.由于未来谷歌政策的变化,文件访问仍然可能被拒绝,所以测试这个值是没有用的。 The only way I found is to test if the external storage root directory is readable, but for that, the app must first ask for Storage permission, which is useless for anything else, if legacy storage is disabled.
我发现的唯一方法是测试外部存储根目录是否可读,但为此,应用程序必须首先请求存储权限,如果禁用旧版存储,这对其他任何事情都无用。
public static boolean mustUseScopedStorage() {
// Impractical must first ask for useless Storage permission...
File exSD = Environment.getExternalStorageDirectory();
return !exSD.canRead(); // this test works only if Storage permission was granted.
}
I think I have once seen a new API to detect this, but cannot find that article anymore...我想我曾经见过一个新的 API 来检测这个,但再也找不到那篇文章了......
You can use these two methods from android.os.Environment
:您可以使用
android.os.Environment
中的这两种方法:
isExternalStorageLegacy(File path)
Returns whether the shared/external storage media at the given path is a legacy view that includes files not owned by the app.
返回给定路径的共享/外部存储媒体是否是包含不属于应用程序的文件的旧视图。
isExternalStorageLegacy()
Returns whether the primary shared/external storage media is a legacy view that includes files not owned by the app.
返回主要共享/外部存储媒体是否是包含不属于应用程序的文件的旧视图。
This value may be different from the value requested by
requestLegacyExternalStorage
in the app's manifest, since an app may inherit its legacy state based on when it was first installed.此值可能与应用清单中
requestLegacyExternalStorage
请求的值不同,因为应用可能会根据首次安装时间继承其旧版 state。Non-legacy apps can continue to discover and read media belonging to other apps via
MediaStore
.非旧版应用程序可以继续通过
MediaStore
发现和读取属于其他应用程序的媒体。
In case you discover that you have access to legacy storage, personally I think it's better to just migrate data over to scoped storage and use it instead since legacy storage may stop working without warning.如果您发现您可以访问旧存储,我个人认为最好将数据迁移到范围存储并改用它,因为旧存储可能会在没有警告的情况下停止工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.