Android getFilesDir() 与 Environment.getDataDirectory()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21230629/
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-20 04:10:28  来源:igfitidea点击:

getFilesDir() vs Environment.getDataDirectory()

android

提问by Elad Benda2

I want to replace one context method with Environment variable.
But not sure if they mean the same.

我想用环境变量替换一种上下文方法。
但不确定它们的意思是否相同。

What is the difference between

之间有什么区别

getFilesDir()and Environment.getDataDirectory()?

getFilesDir()Environment.getDataDirectory()

How can I get data\dataas a from of data\data\com.Myappusing Environment Variable?

我怎样才能data\datadata\data\com.Myapp使用中获得Environment Variable

回答by GrIsHu

From the HEREand HERE

这里这里

public File getFilesDir ()

公共文件 getFilesDir()

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int)are stored.

返回文件系统上用于存储创建的文件的目录的绝对路径openFileOutput(String, int)

public static File getExternalStorageDirectory ()

公共静态文件 getExternalStorageDirectory()

Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

返回主要的外部存储目录。如果该目录已由用户安装在其计算机上、已从设备中删除或发生了其他一些问题,则当前可能无法访问该目录。您可以使用 确定其当前状态getExternalStorageState()

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

注意:不要被这里的“外部”一词混淆。这个目录最好被认为是媒体/共享存储。它是一个文件系统,可以保存相对大量的数据,并在所有应用程序之间共享(不强制执行权限)。传统上这是一张 SD 卡,但它也可以作为设备中的内置存储实现,该设备不同于受保护的内部存储,并且可以作为文件系统安装在计算机上。

On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.

在有多个用户的设备上(如 UserManager 所述),每个用户都有自己独立的外部存储。应用程序只能访问它们正在运行的用户的外部存储。

If you want to get your application path use getFilesDir()which will give you path /data/data/your package/files

如果你想获得你的应用程序路径使用getFilesDir()它会给你路径 /data/data/your package/files

You can get the path using the Environmentvar of your data/packageusing the

您可以使用获得的路径Environment您的VARdata/package使用

getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();which will return the path from the root directory of your external storage as /storage/sdcard/Android/data/your pacakge/files/data

getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();它将从外部存储的根目录返回路径为 /storage/sdcard/Android/data/your pacakge/files/data

To access the external resources you have to provide the permission of WRITE_EXTERNAL_STORAGEand READ_EXTERNAL_STORAGEin your manifest.

要访问外部资源,你必须提供的许可WRITE_EXTERNAL_STORAGE,并READ_EXTERNAL_STORAGE在您的清单。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Check out the Best Documentation to get the paths of direcorty

查看最佳文档以获取目录路径

回答by Adam Radomski

Enironmentreturns userdata directory. And getFilesDirreturns applicationdata directory.

环境返回用户数据目录。而getFilesDir返回应用程序数据目录。

回答by Nambi

getFilesDir()

获取文件目录()

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。

Environment.getDataDirectory()

Environment.getDataDirectory()

Return the user data directory.

返回用户数据目录。

回答by Bibin

Try this

尝试这个

getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath()