Android 问:如何从特定目录获取图像列表
Android Q: How to get a list of images from a specific directory
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
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