如何在API 29中弃用getExternalStorageDirectory来读写文件? [英] How to read or write file as getExternalStorageDirectory is deprecated in API 29?

查看:1804
本文介绍了如何在API 29中弃用getExternalStorageDirectory来读写文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,我在读取java中的getExternalStorageDirectory时遇到了一些问题,我已阅读

I am new in android development and i am facing some problem to read getExternalStorageDirectory in java, I have read https://developer.android.com/reference/android/os/Environment but can't understand, can someone help me with example code in java.

推荐答案

来自文档,您可以看到:

getExternalStoragePublicDirectory(String type)

在API级别29中不赞成使用此方法.为了提高用户隐私,不建议直接访问共享/外部存储设备.什么时候 一个应用程序定位到Build.VERSION_CODES.Q,由此返回的路径 方法不再可供应用直接访问.应用程序可以继续 通过迁移到来访问存储在共享/外部存储中的内容 其他选择,例如 Context#getExternalFilesDir(String), MediaStore或Intent#ACTION_OPEN_DOCUMENT.

This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

不传递任何参数作为此函数的参数,以将目录作为File对象获取:

Pass nothing as parameter to this function to get your directory as a File object :

context.getExternalFilesDir();

此处上下文"是由this.getContext();

this是Activity的当前对象.使用时请仔细检查示波器.

this is the current object of the Activity. Do check the scope carefully while using it.

重要

要访问内部存储,文件Manifest.permission.WRITE_EXTERNAL_STORAGE和/或Manifest.permission.READ_EXTERNAL_STORAGE "noreferrer"> AndroidManifest.xml .

To access the Internal storage, Manifest.permission.WRITE_EXTERNAL_STORAGE and/or Manifest.permission.READ_EXTERNAL_STORAGE are required in the file AndroidManifest.xml.

可选信息:

  1. 通常,内部存储器具有路径 /sdcard/在Android设备上.这不是真实的路径,而是符号链接.

  1. Usually the internal storage has the path /sdcard/ on Android devices. It's not a real path but a symlink.

这很令人困惑,但是Android中的外部sdcard"实际上意味着内部设备存储,而不是外部可弹出设备外存储卡存储. 另外请注意,真正的外部sdcard无法完全访问

It's confusing but "external sdcard" in Android acutally means the Internal device storage and not the external ejectable out-of-the-device memory card storage. Also note that the real external sdcard cannot be fully access

活动类扩展了

Activity class extends the Context class That's why we can get the context from it.

这篇关于如何在API 29中弃用getExternalStorageDirectory来读写文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆