gpt4 book ai didi

android - 如何从 Environment.DIRECTORY_PICTURE 目录中获取所有图像并显示在我的自定义图库中

转载 作者:行者123 更新时间:2023-11-29 00:54:13 24 4
gpt4 key购买 nike

我正在为我的应用程序制作自定义应用程序厨房。从我的自定义相机捕获图像并将图像保存在 Environment.DIRECTORY_PICTURES 中的 xyz 目录中。我想访问所有这些图像并在自定义厨房回收 View 中显示。我尝试了很多方法但无法获取图像。帮助我修复此功能,在此先感谢。

   File fileX = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(), "xyz");
projection = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

Cursor cursor = getContentResolver().query(FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", fileX),
projection, null, null,null);

// private final String[] projection = new String[]{ MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

if (cursor == null) {
message = handler.obtainMessage();
message.what = commonVariables.ERROR;
message.sendToTarget();
return;
}

ArrayList<Image> temp = new ArrayList<>(cursor.getCount());
File file;
folders = new ArrayList<>();

if (cursor.moveToLast()) {
do {
if (Thread.interrupted()) {
return;
}

long id = cursor.getLong(cursor.getColumnIndexOrThrow(projection[0]));
String name = cursor.getString(cursor.getColumnIndexOrThrow(projection[1]));
String path = cursor.getString(cursor.getColumnIndexOrThrow(projection[2]));
String bucket = cursor.getString(cursor.getColumnIndexOrThrow(projection[3]));
DebugLog.e("MSG: PATH = > " + name );
// file = new File(path);
// DebugLog.e("MSG: Absolute PATH = > " + file.getAbsolutePath());
// if (file.exists()) {
// Image image = new Image(id, name, path, false);
// temp.add(image);
//
// if (folderMode) {
// Folder folder = getFolder(bucket);
// if (folder == null) {
// folder = new Folder(bucket);
// folders.add(folder);
// }
//
// folder.getImages().add(image);
// }
// }

它给我参数异常 _ID 不存在。

最佳答案

试试这个解决方案:

private void getImages() {
String uri = MediaStore.Images.Media.DATA;
// if GetImageFromThisDirectory is the name of the directory from which image will be retrieved
String condition = uri + " like '%/xyz/%'";
String[] projection = {uri, MediaStore.Images.Media.DATE_ADDED,
MediaStore.Images.Media.SIZE};
try {
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
condition, null, null);
if (cursor != null) {
boolean isDataPresent = cursor.moveToFirst();
if (isDataPresent) {
do {
Log.e("ImagePath>>>", cursor.getString(cursor.getColumnIndex(uri)));
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

不要忘记在 AndroidManifest.xml 中设置权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 如何从 Environment.DIRECTORY_PICTURE 目录中获取所有图像并显示在我的自定义图库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56286069/

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