gpt4 book ai didi

安卓 Q : How to get a list of images from a specific directory

转载 作者:行者123 更新时间:2023-12-04 23:56:37 24 4
gpt4 key购买 nike

Android 问:我需要从我保存图像的特定目录中获取图像列表,并在我的应用程序中显示这些图像。

保存图片代码:

final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + "MyMedia" + File.separator + "Photo";
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
bmp.compress(Bitmap.CompressFormat.JPEG,100, fos);
Objects.requireNonNull(fos).close();
Objects.requireNonNull(fos).flush();

获取图片代码:

Uri externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.MediaColumns.TITLE,
MediaStore.Images.Media.MIME_TYPE,
MediaStore.MediaColumns.RELATIVE_PATH
};
Cursor cursor = context.getContentResolver().query(externalUri, projection, null, null, MediaStore.Images.Media.DATE_TAKEN +" DESC");


int idColumn = cursor.getColumnIndex(MediaStore.MediaColumns._ID);
int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
int relativePathColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.RELATIVE_PATH);

while (cursor.moveToNext()) {
Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getString(idColumn));

}

我需要一个直接查询来获取保存在 RELATIVE_PATH 中的所有图像的列表,(“MyMedia/Photo”)而不添加,如果游标循环中的条件检查 relativePathColumn如果等于“MyMedia/Photo”,因为这个循环适用于用户设备中的所有图像!我们有没有办法直接从我的目录中获取图像列表?

最佳答案

String path = "MyMedia/Photo";           

String selection = MediaStore.Files.FileColumns.RELATIVE_PATH + " like ? " ;

String selectionargs []= new String[]{"%" + path + "%"};

.query(externalUri, projection, selection, selectionars, MediaStore.Images.Media.DATE_TAKEN

关于安卓 Q : How to get a list of images from a specific directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024543/

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