以编程方式测试是否在Android 10+中禁用了旧版外部存储访问 [英] Test programmatically if in Android 10+ the legacy external storage access is disabled

查看:45
本文介绍了以编程方式测试是否在Android 10+中禁用了旧版外部存储访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个简单的API调用可以告诉我在Android 10或更高版本上运行的应用是否必须使用范围存储访问权限,即是否禁用了旧版外部存储"(又称为旧版外部存储")?请注意,即使应用程序位于AndroidManifest.xml中,该节也声明为:

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, a.k.a. "Legacy External Storage" is disabled? Note that even if the app in AndroidManifest.xml, section declared:

  android:requestLegacyExternalStorage="true"

由于将来Google政策的更改,文件访问仍可能会被拒绝,因此测试该值是没有用的.我发现的唯一方法是测试外部存储根目录是否可读,为此,应用程序必须首先请求存储许可,如果禁用了旧存储,则对其他任何东西都没有用.

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.
}

我想我曾经见过一个新的API可以检测到这一点,但现在找不到该文章了...

I think I have once seen a new API to detect this, but cannot find that article anymore...

推荐答案

您可以从 android.os.Environment :

返回给定路径上的共享/外部存储媒体是否为旧版视图,其中包含该应用程序不拥有的文件.

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.

  • 此值可能与应用清单中的 requestLegacyExternalStorage 请求的值不同,因为应用可能会基于首次安装的时间继承其旧状态.

    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.

    非传统应用可以通过 MediaStore 继续发现和读取属于其他应用的媒体.

    Non-legacy apps can continue to discover and read media belonging to other apps via 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.

    这篇关于以编程方式测试是否在Android 10+中禁用了旧版外部存储访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆