I am making custom application galley for my application. Capture image from my custom camera and save image at xyz directory which is placed in Environment.DIRECTORY_PICTURES. i want to access all those images and display in custom galley recyclerview. i try many ways but unable to get images. help me to fix this functionality thanks in advance.
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);
// }
// }
It is giving me Argument Exception _ID not exist.
Try this solution:
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();
}
}
Don't forgot set permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />