简体   繁体   English

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

[英]`/storage/emulated/legacy/` vs `/storage/emulated/0/` vs `data/data/myApp'

I want to save in my app an imageFile 我想在我的应用程序中保存一个imageFile

I want "Google+ cropper app" to use it. 我希望“Google+ cropper app”能够使用它。

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 shell is 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'

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. 内部: Android设备的内部闪存,用于为每个应用分配私有存储。 The storage allocated is protected to prevent access by any other app (except on rooted devices). 分配的存储受到保护,以防止任何其他应用程序访问(root用户设备除外)。

External: In many cases an SD card with no security restrictions, ie, 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. 随着Android的新版本的出现以及新设备内部闪存越来越多,内部和外部之间的差异正在变得模糊。 For example my Nexus 7 doesn't have an SD card slot. 例如,我的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. 对于没有真正外部存储的设备,Android仍然需要提供模拟的外部存储,以便与旧版应用保持兼容。 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. 换句话说,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()那么您将访问私有分配给您的应用程序的内部存储的根目录,并且只能访问您的应用程序(尽管如我所提到的,根电话可以访问私有/内部存储)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM