如何在android Q中的specifics文件夹上授予完全的写入/读取文件访问权限 [英] how to grant full Write/Read files access on specifics folder in android Q
问题描述
我注意到我在Android Q上的程序无法创建任何文件夹或保存并打开文件.
我已经阅读了Google的文档,但没有找到答案,或者至少我不理解,因为其中有非常小的示例代码,并且据我所知,每次创建文件时,都需要打开一个特殊的窗口
有人可以告诉我如何获得对SD卡中特殊文件夹的完整访问权限?我不想每次都打开一个不同的窗口并确认
长期而言,您无法强迫用户向您提供对外部存储中 test/
目录的访问权限.>
您有三个主要选择.
您可以使用 getExternalFilesDir()
, getExternalCacheDir()
或 getExternalMediaDir()
.这些是 Context
上的方法,它们每个都返回一个 File
,该文件指向您具有完全读写文件系统访问权限的外部存储上的目录.从用户的角度来看,它们将位于 Android/data/.../
下,其中 ...
是您的应用程序ID.
或者,您可以使用 ACTION_OPEN_DOCUMENT_TREE
和Storage Access Framework.将 startActivityForResult()
与 ACTION_OPEN_DOCUMENT_TREE
Intent
一起使用将为您提供 Uri
,它指向文档树,这可能会在外部存储,可移动存储或云中.您可以在 ContentResolver
上调用 takePersistableUriPermission()
以获取对该树的长期访问,还可以使用 DocumentFile.fromTreeUri()
与那棵树一起工作.但是,这不是文件系统访问权限-它更接近于使用Web服务器.
或者,您可以将 android:requestLegacyExternalStorage ="true"
添加到清单中的< application>
元素中.这将为您提供Android Q设备上的旧文件系统行为.按计划将其删除以用于Android R,因此这不是一个合适的长期解决方案.
我在博客中谈到了今年Android Q存储的巨大变化;这是我的最新文章?.
I noticed that my program on Android Q can not create any folder or save and open a file.
I've read Google's documentation but I did not find the answer or at least I did not understand Because there is a very small sample code, and as I figured out, every time you create a file, you need to open a special window
Someone can tell me how can I get a full access for special folder in sdcard?
I do not want to open a different window every time and confirm it
Long term, you have no means of forcing the user to give you access to a test/
directory on external storage.
You have three main options.
You can use getExternalFilesDir()
, getExternalCacheDir()
, or getExternalMediaDir()
. These are methods on Context
, and they each return a File
pointing to a directory on external storage for which you have full read-write filesystem access. From the user's standpoint, these will be under Android/data/.../
, where ...
is your application ID.
Or, you can use ACTION_OPEN_DOCUMENT_TREE
and the Storage Access Framework. Using startActivityForResult()
with an ACTION_OPEN_DOCUMENT_TREE
Intent
will give you a Uri
that points to a document tree, which may be on external storage, removable storage, or in the cloud. You can call takePersistableUriPermission()
on a ContentResolver
to get long-term access to that tree, and you can use DocumentFile.fromTreeUri()
to work with that tree. However, this is not filesystem access — it is a bit closer to working with a Web server.
Or, you can add android:requestLegacyExternalStorage="true"
to your <application>
element in the manifest. This will give you legacy filesystem behavior on Android Q devices. This is scheduled to be removed for Android R, so this is not a suitable long-term solution.
I blogged about the Android Q storage changes a lot this year; here is my last(?) post on it.
这篇关于如何在android Q中的specifics文件夹上授予完全的写入/读取文件访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!