简体   繁体   English

如果在 Android 10+ 中禁用旧版外部存储访问,则以编程方式测试

[英]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.

相关问题 限制访问外部存储,android < 10 - Restricted access to external storage, android < 10 通过在带有目标 SDK 29 的 Android 10 上启用传统外部存储,究竟是什么选择退出范围存储 - what exactly opt out from scoped storage by enabling the legacy external storage on Android 10 with target SDK 29 如何在 Android 10 (Android Q) 中访问外部存储? - How to get Access to External Storage in Android 10 (Android Q)? 使用 Android 10+ 直接访问文件(SQlite 数据库) - Direct file access (SQlite database) with Android 10+ 访问下载文件夹中另一个应用程序的文件 - Android 10+ - Access files of another App inside the Download folder - Android 10+ 使用 ADB (Android 10) 从计算机访问外部存储的直接方式 - Direct Way to Access External Storage from Computer with ADB (Android 10) Android Access外部存储 - Android Access External Storage Android 范围存储 (10+) 从图库中挑选 - Android Scoped Storage (10+) Cross-Profile Pick From Gallery 如何以编程方式拒绝访问外部存储? - How to deny to access to external storage programmatically? GetExternalStoragePublicDirectory Android 10+ 升级问题:试图将 Bitmap 从外部应用程序(OfficeLens)创建到我的应用程序中 - GetExternalStoragePublicDirectory Android 10+ upgrade issue: trying to get the Bitmap created from external app (OfficeLens) into my app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM