android协程作用域_探索android中的作用域存储

android协程作用域

If you see the recent improvements on the Android OS level, the team is mostly focused on security and privacy. For instance, to provide secure preference storage in Android, the team has recently released the EncryptedSharedPreferences library. Regarding sensitive data permissions like location and microphone, single-time permissions — where the apps have access only for that instance — came to light.

如果您看到Android操作系统级别的最新改进,则该团队主要关注安全性和隐私性。 例如,为了在Android中提供安全的首选项存储,该团队最近发布了EncryptedSharedPreferences库 。 关于诸如位置和麦克风之类的敏感数据权限,一次性权限(应用只能访问该实例的权限)暴露无遗。

Finally, as it relates to storage, we got scoped storage, where the entire library access of media is only available to whitelisted apps in Play Console.

最后,与存储相关,我们获得了范围存储,其中,媒体的整个库访问权限仅适用于Play控制台中列入白名单的应用。

问题 (Problem)

The Android storage system has two problems that lead to creating a new storage system even after a decade of its invention:

Android存储系统存在两个问题,即使在其发明十年后仍会导致创建新的存储系统:

  1. It’s pretty much easy for any Android app that has READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions to access your media files and use them.

    对于任何具有READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限的Android应用来说,访问媒体文件和使用它们都非常容易。

  2. The majority of the apps that request storage permissions don’t need a broad view of the media files. Take a chat application where you want to upload media or write a media file into the system. The app only needs access to a single media file, but in the present ecosystem, the entire media collection is exposed to the app.

    大多数请求存储权限的应用程序不需要广泛的媒体文件视图。 将聊天应用程序放在要上传媒体或将媒体文件写入系统的位置。 该应用程序仅需要访问一个媒体文件,但是在当前的生态系统中,整个媒体库都可以访问该应用程序。

解决方案—范围存储 (Solution — Scoped Storage)

Scoped storage is here to save the day. It limits the exposure of the media files based on the app’s requirement. Starting from Android 10, you won’t be able to access the entire media collection without being whitelisted in the Play Console. It also provides a new media framework where we can perform simple tasks, such as accessing a media file in a chat app and saving files into the system without needing any storage permissions.

范围存储在这里可以节省一天。 它根据应用程序的要求限制媒体文件的显示。 从Android 10开始,您必须先在Play控制台中将其列入白名单,然后才能访问整个媒体集。 它还提供了一个新的媒体框架,我们可以在其中执行简单的任务,例如访问聊天应用程序中的媒体文件并将文件保存到系统中,而无需任何存储权限。

Not all companies have enough developers dedicated to focusing on new changes like scoped storage. So the Android team has decided to support both the new scoped storage and the old native storage system until the next Android release. With that said, apps targeting API level 29 have to include the requestLegacyExternalStorage flag in the manifest file to work with the old storage system. Have a look:

并非所有公司都有足够的开发人员专注于诸如范围存储之类的新变化。 因此,Android团队已决定在下一个Android版本发布之前都支持新的作用域存储和旧的本机存储系统。 话虽如此,面向API级别29的应用必须包含requestLegacyExternalStorage 清单文件中的标记以与旧的存储系统一起使用。 看一看:

<applicationandroid:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="@style/AppTheme">
</application>

如何使用范围存储 (How to Use Scoped Storage)

As I said, there are two ways to access media using scoped storage:

就像我说的那样,有两种方法可以使用作用域存储访问媒体:

  • The apps that only require a media file to save it as a profile picture or to use it as an attachment in an email.

    仅需要媒体文件将其另存为个人资料图片或将其用作电子邮件附件的应用程序。
  • The apps that require access to all the media files (e.g. music players and file explorers).

    需要访问所有媒体文件的应用程序(例如音乐播放器和文件浏览器)。

简单用法 (Simple Usage)

Scoped storage has made it easy for apps that only require the media file that was selected by the user (read data from storage) or save a media file to storage (write file).

范围存储使仅需要用户选择的媒体文件(从存储中读取数据)或将媒体文件保存到存储(写入文件)的应用程序变得容易。

读取文件 (Read files)

Here, we use an Intent to start a new activity where users can select a media file, and in the onActivityResult, we can get the file URI. Have a look:

在这里,我们使用一个Intent来启动一个新的活动,用户可以在其中选择一个媒体文件,在onActivityResult ,我们可以获取文件URI 。 看一看:

ACTION_OPEN_DOCUMENT is used as an action to start an activity that presents media files of specified types to the user, and based on user selection, the data is available in onActivityResult (as shown above).

ACTION_OPEN_DOCUMENT用作启动向用户呈现指定类型的媒体文件的活动的操作,并且基于用户的选择,该数据在onActivityResult可用(如上所示)。

CATEGORY_OPENABLE indicates that the intent only requires URIs that can be opened with contentResolver.

CATEGORY_OPENABLE表示该意图仅需要可以使用contentResolver打开的URI。

写文件 (Write files)

It is the same as with reading files. We have to start intent and observe the result in the onActivityResult. Have a look:

与读取文件相同。 我们必须开始意图并在onActivityResult观察结果。 看一看:

Everything is the same as above except for the action type here. We use ACTION_CREATE_DOCUMENT to indicate the system to create the file and revert the URI of it, and we also have to specify the type of the file. We can set the name of the file using the EXTRA_TITLE flag on the intent as well.

除此处的操作类型外,其他一切与上面相同。 我们使用ACTION_CREATE_DOCUMENT来指示系统创建文件并还原其URI ,并且还必须指定文件的类型。 我们也可以在意图上使用EXTRA_TITLE标志来设置文件名。

The system will create this file under the app’s private storage so that we can access them even without storage permission, which is a remarkable thing about scoped storage.

系统将在应用程序的私有存储下创建该文件,以便即使没有存储许可也可以访问它们,这对于范围存储来说是一件了不起的事情。

复杂用法 (Complex Usage)

For apps like the music player and file explorers, accessing a single file isn’t enough with their functionality. READ_EXTERNAL_STORAGE permission is mandatory, as they need to access every file in storage. By using the following piece of code, we can access the list of files:

对于音乐播放器和文件浏览器之类的应用程序,仅凭其功能访问一个文件是不够的。 READ_EXTERNAL_STORAGE权限是强制性的,因为他们需要访问存储中的每个文件。 通过使用以下代码,我们可以访问文件列表:

媒体元数据 (Media Metadata)

Scoped storage has taken measures to protect the metadata of the media that might potentially expose the location history of the user. So the media files that you access through scoped storage only provide the metadata if the app has the necessary permissions.

范围存储已采取措施来保护可能潜在暴露用户位置历史记录的媒体元数据。 因此,如果应用程序具有必要的权限,则通过作用域存储访问的媒体文件仅提供元数据。

Thank you for reading.

感谢您的阅读。

翻译自: https://medium.com/better-programming/exploring-scoped-storage-in-android-f309a7208baa

android协程作用域

你可能感兴趣的:(android,安卓,android,studio,sql)