getFilesDir()与Environment.getDataDirectory() [英] getFilesDir() vs Environment.getDataDirectory()

查看:263
本文介绍了getFilesDir()与Environment.getDataDirectory()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要替换环境变量一个上下文方法。
但是,不知道他们的意思是一样的。

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

getFilesDir() Environment.getDataDirectory()

我怎样才能获得数据\数据作为从数据\数据\ com.Myapp <​​/ code>使用环境变量

How can I get data\data as a from of data\data\com.Myapp using Environment Variable?

推荐答案

在<一个href="http://developer.android.com/reference/android/content/ContextWrapper.html#getFilesDir%28%29">HERE和<一href="http://developer.android.com/reference/android/os/Environment.html#getDataDirectory%28%29">HERE

公开文件getFilesDir()

返回的绝对路径在哪里与 openFileOutput(字符串,INT)存储中创建文件的文件系统的目录。

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

公共静态文件getExternalStorageDirectory()

返回主外部存储目录。这种目录可能当前不被访问,如果它已经被安装通过计算机上的用户,已经从设备中移出,或一些其它的问题发生了。您可以确定与 getExternalStorageState其当前状态()

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().

请注意:千万不要被这个词迷惑外部在这里。这个目录可以更好地被认为是媒体/共享存储。它是一个文件系统,可容纳相对大量的数据,并且是在所有的应用程序(不强制权限)共享。传统上,这是一SD卡,但作为内置存储在一个设备,它是从受保护的内部存储不同且可被安装为计算机上一个文件系统它也可以被实现。

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.

在(如由的UserManager)与多个用户装置,每一个用户都有自己的分离的外部存储器。应用程序只能访问外部存储用于他们正在运行的用户。

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.

如果你想获得你的应用程序路径中使用 getFilesDir(),这将给你路径 /数据/数据​​/软件包/文件

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

您可以使用环保变种的数据/包用得到的路径

You can get the path using the Environment var of your data/package using the

<$c$c>getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();这将返回的路径从外部存储作为根目录 /存储/ SD卡/安卓/数据/你pacakge /文件/数据

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

要访问外部资源,你必须提供 WRITE_EXTERNAL_STORAG​​E READ_EXTERNAL_STORAG​​E 在你的清单。

To access the external resources you have to provide the permission of WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE in your manifest.

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

查看最佳文档,以获得direcorty的路径

这篇关于getFilesDir()与Environment.getDataDirectory()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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