如何从 Environment.DIRECTORY_PICTURE 中的目录中获取所有图像并显示在我的自定义图库中
How can I get all images from directory in Environment.DIRECTORY_PICTURE and display in my custom gallery
我正在为我的应用程序制作自定义应用程序样板。从我的自定义相机捕获图像并将图像保存在 Environment.DIRECTORY_PICTURES 中的 xyz 目录中。我想访问所有这些图像并在自定义厨房回收视图中显示。我尝试了很多方法但无法获取图像。帮助我修复此功能,在此先感谢。
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" />
我正在为我的应用程序制作自定义应用程序样板。从我的自定义相机捕获图像并将图像保存在 Environment.DIRECTORY_PICTURES 中的 xyz 目录中。我想访问所有这些图像并在自定义厨房回收视图中显示。我尝试了很多方法但无法获取图像。帮助我修复此功能,在此先感谢。
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" />