我正在为我的应用程序制作自定义应用程序厨房。从我的自定义相机捕获图像并将图像保存在Environment.DIRECTORY_PICTURES中的xyz目录中。我想访问所有这些图像并在自定义厨房recyclerview中显示。我尝试了很多方法但无法获取图像。谢谢,帮助我修复此功能。
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不存在。
答案 0 :(得分:1)
尝试以下解决方案:
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" />