gpt4 book ai didi

android - 如何获取相机文件的二级外部目录路径

转载 作者:行者123 更新时间:2023-11-29 20:30:17 24 4
gpt4 key购买 nike

我有一个带 SD 卡的设备。现在我想检查设备是否安装了外部 SD 卡并且可以从公共(public) DCIM 文件夹读取文件。我知道我可以使用 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);,但此方法仅返回主外部存储器上的文件,而不是安装的 SD 卡(辅助外部存储器)上的文件。

我发现 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 返回索引为 1 的元素,用于辅助外部存储,但此方法仅获取应用程序沙箱的文件(Android/data/packagename ).所以我的问题是如何获取公共(public)目录(如 DCIM)的辅助外部路径的路径?

最佳答案

我找到了解决方案,这里是代码 fragment :

String strSDCardPath = System.getenv("SECONDARY_STORAGE");

if ((strSDCardPath == null) || (strSDCardPath.length() == 0)) {
strSDCardPath = System.getenv("EXTERNAL_SDCARD_STORAGE");
}

//If may get a full path that is not the right one, even if we don't have the SD Card there.
//We just need the "/mnt/extSdCard/" i.e and check if it's writable
if(strSDCardPath != null) {
if (strSDCardPath.contains(":")) {
strSDCardPath = strSDCardPath.substring(0,strSDCardPath.indexOf(":"));
}
File externalFilePath = new File(strSDCardPath);

if (externalFilePath.exists() && externalFilePath.canWrite()) {
//do what you need here
}
}

有关详细信息,请阅读此处:Finding the SDCard Path on Android devices

关于android - 如何获取相机文件的二级外部目录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32072095/

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