gpt4 book ai didi

android - 为什么我的安卓设备/mnt下有两个文件夹sdcard和sdcard2

转载 作者:行者123 更新时间:2023-11-29 01:49:34 24 4
gpt4 key购买 nike

当使用 Eclipse 文件资源管理器浏览我的 android 目录时,我看到了 mnt/sdcardmnt/sdcard2,见下图:

调用Environment.getExternalStorageDirectory()时返回mnt/sdcard,所以我认为mnt/sdcad外部storagemnt/sdcard2 是我真正的 SD 卡,是这样吗?以及如何使用代码访问 mnt/sdcard2 下的文件?

enter image description here

附言

看来我是可以直接访问外置sd卡的:

    File extStorageDir = Environment.getExternalStorageDirectory();
String parent = extStorageDir.getParent();
File extSdCardDir = new File(parent+"/sdcard2");

File file = new File(extSdCardDir, "DemoFile.jpg");

但我不知道额外的sd卡在其他情况下会更改名称。

最佳答案

你是对的,getExternalStorage 将返回你的内置外部存储。不幸的是,如果设备还具有内置存储和 SD 卡,则 Jelly Bean 应用程序将无法再使用 SD 卡。您可以尝试通过 shell 命令或硬编码路径解决它,但如果没有 root,就没有可靠的方法来访问它了。

这是最近才添加的 Android CTS,所有 OEM 都必须遵守它才能使用 Play 商店。

Compatibility Program Overview | Android Developers

Section 9.5 (pg. 34) of Android 4.3 Compatibility Definition

Device implementations that include multiple external storage paths MUST NOT allow Android applications to write to the secondary external storage.

Storage Options | Android Developers

It's possible that a device using a partition of the internal storage for the external storage may also offer an SD card slot. In this case, the SD card is not part of the external storage and your app cannot access it (the extra storage is intended only for user-provided media that the system scans).

Android 4.2 APIs | Android Developers

Saving data in a multi-user environment

Whenever your app saves user preferences, creates a database, or writes a file to the user’s internal or external storage space, that data is accessible only while running as that user.

To be certain that your app behaves properly in a multi-user environment, do not refer to your internal app directory or external storage location using hard-coded paths and instead always use the appropriate APIs:

For access to internal storage, use getFilesDir(), getCacheDir(), or openFileOutput().
For access to external storage, use getExternalFilesDir() or getExternalStoragePublicDirectory().

No matter which of these APIs you use to save data for a given user, the data will not be accessible while running as a different user. From your app’s point of view, each user is running on a completely separate device.

关于android - 为什么我的安卓设备/mnt下有两个文件夹sdcard和sdcard2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19087355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com