Android `/storage/emulated/legacy/` vs `/storage/emulated/0/` vs `data/data/myApp'

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

`/storage/emulated/legacy/` vs `/storage/emulated/0/` vs `data/data/myApp'

androidstorage

提问by Elad Benda

I want to save in my app an imageFile

我想在我的应用程序中保存一个 imageFile

I want "Google+ cropper app" to use it.

我想要“Google+ 裁剪应用”来使用它。

But the later opens another image.

但后者打开了另一个图像。

I guess it's permissions issue.

我猜是权限问题。

In my code i save here:

在我的代码中,我保存在这里:

Is this external storage? Environment.getExternalStorageDirectory().getAbsolutePath()which returns: /storage/emulated/0/myApp/file1.tmp

这是外部存储吗? Environment.getExternalStorageDirectory().getAbsolutePath()返回:/storage/emulated/0/myApp/file1.tmp

using adb shellis see the file is actually saved in: /storage/emulated/legacy/myApp/file1.tmp

使用adb shell是查看文件实际保存在:/storage/emulated/legacy/myApp/file1.tmp

why is the difference?

为什么有区别?

should i use this place instead? Is this external storage?

我应该改用这个地方吗?这是外部存储吗?

getAppContext().getFilesDir().getParent()which returns: `data/data/myApp'

getAppContext().getFilesDir().getParent()返回:`data/data/myApp'

回答by Squonk

Historically the difference between internal and external storage was as follows...

从历史上看,内部和外部存储之间的区别如下...

Internal:The internal flash storage of an Android device used to allocate private storage for each app. The storage allocated is protected to prevent access by any other app (except on rooted devices).

内部:Android 设备的内部闪存,用于为每个应用分配私有存储空间。分配的存储受到保护,以防止任何其他应用程序访问(root 设备除外)。

External:In many cases an SD card with no security restrictions, i.e., all apps can access all areas of "external" storage.

外部:在许多情况下,没有安全限制的 SD 卡,即所有应用程序都可以访问“外部”存储的所有区域。

As new versions of Android have come along and new devices have increasingly more internal flash storage, the difference between internal and external is becoming blurred. For example my Nexus 7 doesn't have an SD card slot.

随着新版本 Android 的出现和新设备的内部闪存越来越多,内部和外部之间的区别变得越来越模糊。例如,我的 Nexus 7 没有 SD 卡插槽。

In the case of devices without true external storage, it's still necessary for Android to provide an emulated external storage in order to remain compatible with older apps. In other words the RAM is physically internal (non-removable) but a section of it is partitioned and the Android file-system APIs treat that partition as being "external" and world-readable.

对于没有真正外部存储的设备,Android 仍然需要提供模拟的外部存储以保持与旧应用程序的兼容。换句话说,RAM 在物理上是内部的(不可移动的),但它的一部分是分区的,Android 文件系统 API 将该分区视为“外部”和世界可读的。

As for the paths you see for external storage such as...

至于您看到的外部存储路径,例如...

/storage/emulated/0/myApp/file1.tmp
/storage/emulated/legacy/myApp/file1.tmp

...one or other of those (possibly both) is a redirection or "virtual" path to the same part of the emulated external directory and file.

...其中一个(可能两者)是指向模拟外部目录和文件的同一部分的重定向或“虚拟”路径。

This is why it is essential to always use the correct API call to get access to files and directories rather than assuming a hard-coded path as it may well vary from device to device.

这就是为什么必须始终使用正确的 API 调用来访问文件和目录而不是假设硬编码路径,因为它可能因设备而异。

If you use Environment.getExternalStorageDirectory(), you can be confident that any other app which does the same will be able to get access to any files you create there.

如果您使用Environment.getExternalStorageDirectory(),您可以确信任何其他执行相同操作的应用程序将能够访问您在那里创建的任何文件。

If you use getFilesDir()then you are accessing the root of the internal storage allocated privately to your app and accessible only to your app (although, as I mentioned a rooted phone can access private / internal storage).

如果您使用,getFilesDir()那么您将访问私有分配给您的应用程序的内部存储的根目录,并且只能由您的应用程序访问(尽管,正如我提到的,有 root 权限的手机可以访问私有/内部存储)。