I want to query all of the files in a folder called as memories
through the Mediastore API. I have tried looking at these questions but didn't get an appropriate answer.
- https://android.jlelse.eu/handling-files-in-code-after-the-android-10-released-2bea0e16d35
- https://developer.android.com/training/data-storage/shared/media#query-collection
- How to I read and write a txt file from android 10 and above from internal storage
- How to get the path of a specific folder in Android Q
- How do I list all file in a directory in android 10 using Android Studio?
EDIT: Abs Path: imageUri: content://media/external/images/media/31
EDIT: How I save the image:
private void saveImage(Bitmap bitmap, @NonNull String name) throws IOException {
boolean saved;
OutputStream fos;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
Log.d(TAG, "imageUri: " + imageUri);
fos = resolver.openOutputStream(imageUri);
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;
File file = new File(imagesDir);
if (!file.exists()) {
file.mkdir();
}
File image = new File(imagesDir, name + ".png");
Log.d(TAG, "File file: " + image);
fos = new FileOutputStream(image);
}
saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.d(TAG, "Bitmap Image AndroidQ: " + saved);
fos.flush();
fos.close();
}
EDIT:
I looked up many questions with the tag Mediastore
and formed this solution but I am not able to write SQLite Placeholder syntax so can anyone help me with that. @blackapps
String selection = MediaStore.MediaColumns.RELATIVE_PATH + "/DCIM/" + IMAGES_FOLDER_NAME;
String[] selectionArgs = new String[] {"%memories%"};
try (Cursor cursor = requireContext().getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null
)) {
assert cursor != null;
int nameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
Log.d(TAG, "name column: " + nameColumn);
while (cursor.moveToNext()) {
String name = cursor.getString(nameColumn);
byte[] bytes = name.getBytes();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.d(TAG, "file name: " + name);
imageList.add(bitmap);
}
}