gpt4 book ai didi

android - 获取主存储和二级存储上 DCIM 文件夹的路径

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:06 25 4
gpt4 key购买 nike

我正在编写一个应用程序,它应该上传存储在内存和 SD 卡上的 DCIM/Camera 文件夹中的相机拍摄的照片。

这意味着在每次上传之前,必须检查所有可用存储空间是否存在任何图像。

我使用 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) 访问主存储,它可以是 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.

我的问题是,如何在不对路径进行硬编码的情况下访问辅助存储来检查 DCIM/Camera 文件夹中是否存在图像,这不能很好地工作,因为 SD 卡可能会在不同的路径中被模拟。

最佳答案

尝试使用此代码选择上次使用的 DCIM/Camera 文件夹。

String getDCIMCamera() {
try {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE};

Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor != null) {
cursor.moveToFirst();
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
if (path.contains("/DCIM/")) {
File file = new File(path);
path = file.getParent();
cursor.close();
return path;
}
} while (cursor.moveToNext());
cursor.close();
}
}
catch (Exception e) {
}
return "";
}

另见@Pedram 提示:https://stackoverflow.com/a/24189813/966789

关于android - 获取主存储和二级存储上 DCIM 文件夹的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29576098/

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