Java 在 API 29 中不推荐使用 getExternalStorageDirectory 时,如何读取或写入文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56819558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:17:01  来源:igfitidea点击:

How to read or write file as getExternalStorageDirectory is deprecated in API 29?

javaandroidandroid-10.0

提问by Mujahid khan

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/Environmentbut can't understand, can someone help me with example code in java.

我是 android 开发的新手,我在 java 中读取 getExternalStorageDirectory 时遇到了一些问题,我已经阅读了https://developer.android.com/reference/android/os/Environment但无法理解,有人可以帮助我举个例子java中的代码。

采纳答案by Crazo7924

From the docsyou can see:

文档中您可以看到:

getExternalStoragePublicDirectory(String type)

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.

getExternalStoragePublicDirectory(String type)

此方法在 API 级别 29 中已弃用。为了提高用户隐私,不推荐直接访问共享/外部存储设备。当应用以 Build.VERSION_CODES.Q 为目标时,应用无法再直接访问从此方法返回的路径。通过迁移到Context#getExternalFilesDir(String)、MediaStore 或 Intent#ACTION_OPEN_DOCUMENT等替代方案,应用程序可以继续访问存储在共享/外部存储上的内容

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

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

context.getExternalFilesDir();

context.getExternalFilesDir();

Here "Context" is an object which is obtained by this.getContext();

这里的“上下文”是一个对象,它是通过 this.getContext();

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

this是活动的当前对象。使用时请仔细检查示波器。

Important

重要的

To access the Internal storage, Manifest.permission.WRITE_EXTERNAL_STORAGEand/or Manifest.permission.READ_EXTERNAL_STORAGEare required in the file AndroidManifest.xml.

要访问内部存储,Manifest.permission.WRITE_EXTERNAL_STORAGE和/或Manifest.permission.READ_EXTERNAL_STORAGE在文件AndroidManifest.xml 中是必需的。

Optional information:

可选信息:

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

  2. 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

  3. Activityclass extends the Contextclass That's why we can get the context from it.

  1. 通常,内部存储在 Android 设备上的路径为 /sdcard/。这不是一个真正的路径,而是一个符号链接

  2. 令人困惑的是,Android 中的“外部 sdcard”实际上是指内部设备存储,而不是外部可弹出的设备外存储卡存储。另请注意,真正的外部sdcard无法完全访问

  3. Activity类扩展了Context类,这就是我们可以从中获取上下文的原因。

回答by Neeraj Dhameliya

Use this static method. Currently i don't find any legal way to do this. So, I was made this static method to get root or getAbsolutePath file path.

使用这个静态方法。目前我没有找到任何合法的方法来做到这一点。所以,我使用这个静态方法来获取 root 或 getAbsolutePath 文件路径。

public static File getAbsoluteDir(Context ctx, String optionalPath) {
        String rootPath;
        if (optionalPath != null && !optionalPath.equals("")) {
            rootPath = ctx.getExternalFilesDir(optionalPath).getAbsolutePath();
        } else {
            rootPath = ctx.getExternalFilesDir(null).getAbsolutePath();
        }
        // extraPortion is extra part of file path
        String extraPortion = "Android/data/" + BuildConfig.APPLICATION_ID
                + File.separator + "files" + File.separator;
        // Remove extraPortion
        rootPath = rootPath.replace(extraPortion, "");
        return new File(rootPath);
    }