[英]Android R – Create a text file in Download folder using Scoped storage (MediaStore.Downloads)
My app needs to save a string in a text file in Download folder.我的应用程序需要在下载文件夹中的文本文件中保存一个字符串。 Currently (target: API 29 (Q) I am using the FILE API with:
目前(目标:API 29(Q)我正在使用 FILE API 与:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"
But for API 30 (R), if I understood well, I have to migrate this to Scoped storage (MediaStore.Downloads).但是对于 API 30 (R),如果我理解得很好,我必须将其迁移到范围存储 (MediaStore.Downloads)。
And here, I am a bit lost.在这里,我有点迷路了。 I can't find good documentation or snippet showing how to create a text file in Download.
我找不到好的文档或片段来说明如何在下载中创建文本文件。 I would appriciate if someone could explain or show how to do this?
如果有人可以解释或展示如何做到这一点,我会很感激?
Having just tested using your above setup in a AndrioidManifest.xml
:刚刚在
AndrioidManifest.xml
中使用上述设置进行了测试:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"
With those permissions granted and on a emulator running API 30 (R) I was able to write/read/update a text file with this simple code:授予这些权限并在运行 API 30 (R) 的模拟器上,我能够使用以下简单代码写入/读取/更新文本文件:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
val f = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "new_file.txt")
f.appendText("test ${Instant.now().toEpochMilli()}\n")
f.readLines().forEach { line -> Log.e("LOG", line)}
}
}
This post: https://stackoverflow.com/a/64192581/4252352 suggests that if you created the file you will be able to have access to it using the File api. Note: Environment.getExternalStoragePublicDirectory
is deprecated but seems to work even on Android 11 if you are the owner of the file.这篇文章: https://stackoverflow.com/a/64192581/4252352建议如果您创建了文件,您将能够使用文件 api 访问它。注意:
Environment.getExternalStoragePublicDirectory
已被弃用,但似乎甚至可以工作Android 11 如果您是文件的所有者。
Just for fun I swapped between target and compile versions 29/30 to see if anything would blow up when targetting different sdk's and reinstalling the same app on the same emulator.只是为了好玩,我在目标版本和编译版本 29/30 之间进行了交换,以查看在针对不同的 sdk 并在同一模拟器上重新安装同一应用程序时是否会出现任何问题。 It worked fine, I had full access to the same file regardless.
它工作正常,无论如何我都可以完全访问同一个文件。
If I'm honest the whole thing is a bit of a shambles - this post by CommonsWare https://commonsware.com/blog/2019/12/21/scoped-storage-stories-storing-mediastore.html is a good read as it touches on many things that are now enforced in Android 11, even though it mostly talks about Android 10.老实说,整件事有点混乱——CommonsWare https://commonsware.com/blog/2019/12/21/scoped-storage-stories-storing-mediastore.html的这篇文章值得一读因为它涉及现在在 Android 11 中强制执行的许多事情,尽管它主要谈论 Android 10。
Documentation seems to be splintered with sections pertaining to storage / scoped storage and the like in a lot of different places.文档似乎在许多不同的地方被与存储/范围存储等相关的部分分散在一起。 This link to a table gives a good launch ground for sifting through the documentation based on inital use case: https://developer.android.com/training/data-storage
此表格链接为根据初始用例筛选文档提供了良好的启动平台: https://developer.android.com/training/data-storage
I have also attached a screenshot of the file appearing in the file manager:我还附上了文件管理器中出现的文件的屏幕截图:
PS: Terrible throwaway code here - I/O work on main thread etc.. only for illustrative purposes. PS:这里是可怕的一次性代码——I/O 在主线程等上工作。仅用于说明目的。
Based on comment "R.= 30": My usages of "R" and Api "30" are based on this from the AndroidStudio IDE :基于评论“R.= 30”:我对“R”和 Api“30”的用法基于 AndroidStudio IDE:
(R = runtime environment, Api 30 = sdk for runtime) (R = 运行时环境,Api 30 = sdk 运行时)
Happy for an edit if I have misunderstood something, or not semantically correct in some way.如果我误解了某些东西,或者在某种程度上语义上不正确,很高兴进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.