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.
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);
}
}
You can do like this
fun getFiles(var directory_name){
var selection: String
selection = MediaStore.Files.FileColumns.RELATIVE_PATH+ " =? "
var selectionArgs = arrayOf(directory_name)
val cursor: Cursor? = contentResolver.query(
MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),
null,
selection,
selectionArgs,
null
)
}
you need to iterate cursor with do while loop or any other
function call: getFiles(Lyrics/) //folder name from internal meomery if you want to use sd card then use MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL). I have tested this and its working fine for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.