Scoped Storage Stories: The Undocumented Documents

栏目: IT技术 · 发布时间: 4年前

内容简介:Android 10 isOne apparent gap in theAs it turns out, there

Android 10 is greatly restricting access to external storage via filesystem APIs. Instead, we need to use other APIs to work with content. This is the 12th post in a seemingly never-ending series, where we will explore how to work with those alternatives.

One apparent gap in the MediaStore options is the Documents/ directory. We have obvious support for the media directories and Downloads/ , but everything else had to be handled via the Storage Access Framework. This includes Documents/ , which, like Downloads/ , seems like it would be a general-purpose location.

As it turns out, there is support for Documents/ . It’s just not documented.

This comes to us courtesy of Stack Overflow user blackapps, and this answer .

To work with downloads, as we sawpreviously, we can use MediaStore.Downloads as the basis. However, using MediaStore.Files , we can work with Downlodads/ and Documents/ , plus custom subdirectories under those.

Specifically, MediaStore.Files.getContentUri("external") gets us a Uri that we can use akin to MediaStore.Downloads.EXTERNAL_CONTENT_URI for inserting new content and querying for our own content, though as before we cannot access other apps’ content this way. If we use RELATIVE_PATH and specify Documents , our content will go into the Documents/ directory:

val values = ContentValues().apply {
  put(MediaStore.Files.FileColumns.DISPLAY_NAME, filename)
  put(MediaStore.Files.FileColumns.MIME_TYPE, mimeType)
  put(MediaStore.Files.FileColumns.RELATIVE_PATH, "Documents")
  put(MediaStore.Files.FileColumns.IS_PENDING, 1)
}

val resolver = context.contentResolver
val uri =
  resolver.insert(MediaStore.Files.getContentUri("external"), values)

uri?.let {
  resolver.openOutputStream(uri)?.use { outputStream ->
    val sink = outputStream.sink().buffer()

    response.body?.source()?.let { sink.writeAll(it) }
    sink.close()
  }

  values.clear()
  values.put(MediaStore.Downloads.IS_PENDING, 0)
  resolver.update(uri, values, null, null)
} ?: throw RuntimeException("MediaStore failed for some reason")

We can also use subdirectories under Documents/ , such as Documents/AwesomeStuff . And, we can use Downloads/ as the base. Attempts to write to other root directories than Documents/ and Downloads/ will fail with an error.

You can even use this for removable volumes. blackapps’ answer shows a hard-coded storage volume ID, though presumably using StorageManager and methods like getStorageVolumes() will be more flexible.

The fact that this does not appear to be documented means that it is possible that this will not be supported in future versions of Android. So, use this approach with caution. But, if Documents/ is a must-have location, and you really want to avoid the Storage Access Framework, MediaStore.Files may be something to consider.

The entire series of “Scoped Storage Stories” posts includes posts on:


以上所述就是小编给大家介绍的《Scoped Storage Stories: The Undocumented Documents》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网的基因

互联网的基因

人民邮电出版社 / 2016-9-21 / 48.00元

《互联网的基因》是一本从电信看互联网创新,从互联网看电信创新的力作。作者何宝宏博士长期在电信行业从事互联网领域研究,是极为少有的“既懂IP又懂电信”的专家。该书借以电信和互联网技术创新的大脉络,用轻松、诙谐、幽默的语言,结合经济学、社会学、哲学、人类学甚至心理学理论,揭示互联网、云计算、大数据以及目前最热门的区块链等技术发展背后的规律。作者在该书中明确表示,互联网是新的技术物种,互联网有基因,互联......一起来看看 《互联网的基因》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具