requestLegacyExternalStorage在Android 11中不起作用-API 30 [英] requestLegacyExternalStorage is not working in Android 11 - API 30

查看:113
本文介绍了requestLegacyExternalStorage在Android 11中不起作用-API 30的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google最近引入了一些与API 29中的存储API相关的更改,例如范围存储,我们通过在清单中添加'requestLegacyExternalStorage = true'退出.但是现在当我以SdkVersion 30为目标时,这似乎不再起作用.更改后,下载目录中的某些文件未列出(File.listFiles).

解决方案

但是现在当我定位到SdkVersion 30时,这似乎不再起作用

那是正确的.Android 11(API 30+) requestLegacyExternalStorage = true 不执行任何操作,您无法再选择退出".Android 10中提供了该功能,为开发人员提供了过渡/宽限期,使其能够迁移到范围存储模型.

选项1:在仍然以API 29为目标的情况下迁移应用程序中的数据,那么一旦迁移的数据与范围存储兼容,您就应该能够发布以API 30为目标的更新- https://developer.android.com/reference/android/R.attr#preserveLegacyExternalStorage

展望未来,您可以参考此表来确定存储的框架".根据用例使用: https://developer.android.com/training/data-storage

基于某些应用程序与 File api的交互方式,有些应用程序可能根本无法成功迁移,因为Google的解决方案将无法涵盖所有​​当前用例,并且可能不会迁移路径.

例如,我几年前发布了一个应用,该应用允许用户使用 MediaStore ContentResolver 更新专辑插图的数据-这是存储在共享的内存中.看了Android 10+ aosp MediaProvider 源代码后,似乎曾经使用 MediaStore 更新专辑插图以指向数据文件的应用不再起作用,原因仅在于 MediaProvider 内部在隐藏的 .thumbnails 文件夹中内部创建自己的艺术作品,直接查看mp3并使用 MediaExtractor ,而从不引用 ContentValues 来引用插图.因此,即使您可以自己更新艺术品,查询 MediaStore 并查看它,其他应用也必须在api 29+中使用 ContentResolver#loadThumbnail ,但该参考不会引用您的艺术品更新值并懒惰地创建图稿,或在 .thumbnails 文件夹中选择已经生成的文件.显然,这些都没有得到记录,并且我的应用遭到了负面评价,引起了强烈反响,但是这些更改却打破了更改,完全超出了我的控制范围,并让我查看了AOSP源代码,从而发现Android从根本上改变了行为./p>

(这不是大声疾呼,而是因为由于对AOSP的根本性未记录的行为,这些变化如何没有提供迁移路径的例子).

Google has introduced some changes recently related to storage APIs in API 29 like scoped storage and we opted out by adding 'requestLegacyExternalStorage=true' in Manifest. But now when I targetSdkVersion 30, this no longer seems to work. Some of the files in the download directories were not listing (File.listFiles) after this change.

解决方案

But now when I targetSdkVersion 30, this no longer seems to work

That is correct. Android 11 (API 30+) requestLegacyExternalStorage=true does nothing and you can no longer "opt-out". It was available in Android 10 to give developers a trasition/grace period to be able to migrate to the scoped storage model.

option 1 : Migrate data in your app whilst still targeting API 29, then once you're migrated data is compatiable with scoped storage you should be able to release an update targetting API 30 - https://developer.android.com/preview/privacy/storage#scoped-storage.

This can come with its own problems if users skip this version and updates dirctly from a previous version to the latest and you're stuck with un-migrated data you can't access.

option 2 : It seems that Google sees this obvious caveat and has included a preserveLegacyExternalStorage=true option when targetting API 30 to allow you to migrate data. https://developer.android.com/reference/android/R.attr#preserveLegacyExternalStorage

Going forward you can reference this table for deciding what storage "framework" to use based on the usecase : https://developer.android.com/training/data-storage

There is a potential that some apps simply won't be able to successfully migrate, based on how they interacted with the File api as Google's solution will not encompass all current usecases and there might not be a migration path.

For instance I relased an app a couple years ago that allowed users to update album artwork using MediaStore and ContentResolver to update the data for the album artwork image - this was stored in shared sorage. Having looked at the Android 10+ aosp MediaProvider source code it seems that apps that used to use MediaStore to update album artwork to point to a data file no longer works, simply because the MediaProvider internally creates it's own artwork in a hidden .thumbnails folder looking directly at the mp3's and using a MediaExtractor, and never references the ContentValues that were inserted to reference the artwork. So even though you can update the artwork yourself, query the MediaStore for it and see it, other apps have to use ContentResolver#loadThumbnail in api 29+ that does not reference your updated values and either creates a artwork lazily, or picks the already generated file in the .thumbnails folder. Obviously none of this is documented, and I got a massive backlash to my app with negative reviews, yet these changes were breaking changes and completely out of my control and took me looking through AOSP source code to find that Android had fundamentally changed behaviour.

(This wasn't a rant, but an exmaple of how these changes offered no migration path because of fundemental undocumented behaviour to AOSP).

这篇关于requestLegacyExternalStorage在Android 11中不起作用-API 30的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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