[英]How to request permission to write to removable sd card in Android 5.0 and above
A simple question. 一个简单的问题。 By default android doesn't allow 3rd party apps to write to removable sd card.
默认情况下,Android不允许第三方应用程序写入可移动SD卡。
But in Android 5.0 I can request for permission to write to removable sdcard and write any file in that directory. 但是在Android 5.0中,我可以请求写入可移动sdcard的权限并在该目录中写入任何文件。
So how to do that? 那么该怎么做呢? I need an output stream to write to the file.
我需要一个输出流来写入文件。
I can request for permission to write to external sdcard and write any file in that directory.
我可以请求写入外部sdcard的权限并在该目录中写入任何文件。
Not really. 并不是的。
On Android 4.4+, you can use getExternalFilesDirs()
, getExternalCacheDirs()
, and getExternalMediaDirs()
. 在Android
getExternalCacheDirs()
及getExternalMediaDirs()
版本上,您可以使用getExternalFilesDirs()
, getExternalCacheDirs()
和getExternalMediaDirs()
。 If those return 2+ items, the second and subsequent ones will be locations on removable storage. 如果它们返回2个以上的项目,则第二个和后续项目将是可移动存储中的位置。
On Android 5.0+, you can use ACTION_OPEN_DOCUMENT_TREE
, to ask the user to grant you access to a document tree. 在Android 5.0及更高版本上,您可以使用
ACTION_OPEN_DOCUMENT_TREE
来要求用户授予您对文档树的访问权限。 That could be the root of removable storage... or anything else the user wants to use. 那可能是可移动存储的根本……或用户想要使用的其他任何东西。 This gives you a
Uri
back, which you can use with DocumentFile
and the rest of the Storage Access Framework. 这为您提供了
Uri
,可以将它与DocumentFile
和其余的Storage Access Framework一起使用。 It does not give you filesystem access (eg, via File
). 它不会给你的文件系统访问(例如,通过
File
)。
On Android 7.0+, you can use StorageManager
and StorageVolume
to ask for access to a removable storage volume. 在Android 7.0+上,您可以使用
StorageManager
和StorageVolume
要求访问可移动存储卷。 Once again, though, you get a Uri
back to use with DocumentFile
, ContentResolver
, etc -- you do not get filesystem access. 但是,您再次得到一个
Uri
来与DocumentFile
, ContentResolver
等一起使用-您没有获得文件系统访问权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.