Android Q: How to get a list of images from a specific directory
Asked Answered
T

1

6

Android Q: I need to get a list of images from a specific directory I saved images on it and display these images on my app.

Save images code:

final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + "MyMedia" + File.separator + "Photo";
            ContentResolver resolver = context.getContentResolver();
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
            Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
            bmp.compress(Bitmap.CompressFormat.JPEG,100, fos);
            Objects.requireNonNull(fos).close();
            Objects.requireNonNull(fos).flush();

Get images code:

Uri externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                String[] projection = {
                        MediaStore.Files.FileColumns._ID,
                        MediaStore.Images.Media.DATE_TAKEN,
                        MediaStore.MediaColumns.TITLE,
                        MediaStore.Images.Media.MIME_TYPE,
                        MediaStore.MediaColumns.RELATIVE_PATH
                };
                Cursor cursor = context.getContentResolver().query(externalUri, projection, null, null,  MediaStore.Images.Media.DATE_TAKEN +" DESC");


            int idColumn = cursor.getColumnIndex(MediaStore.MediaColumns._ID);
            int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
            int relativePathColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.RELATIVE_PATH);

            while (cursor.moveToNext()) {
                Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getString(idColumn));

            }

I need a direct query to get list of all images saved in the RELATIVE_PATH , ("MyMedia/Photo") without add, if condition in the cursor loop to check relativePathColumn if equal "MyMedia/Photo", because of this loop for all images in the device of the user! Did we have any way to get a list of images directly from my directory?

Twelvemonth answered 2/2, 2020 at 8:52 Comment(0)
S
7
String path = "MyMedia/Photo";           

String selection = MediaStore.Files.FileColumns.RELATIVE_PATH + " like ? " ;

String selectionargs []= new String[]{"%" + path + "%"};  

.query(externalUri, projection, selection, selectionars,  MediaStore.Images.Media.DATE_TAKEN 
Sigler answered 2/2, 2020 at 9:48 Comment(5)
Thank you, the Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getString(idColumn)); return path like this : content:/media/external/images/media/43 How I can return the real path to display itTwelvemonth
You do not need 'a real path' to display an image. Just use your uri, your content scheme.Sigler
InputStream is = getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(is);Sigler
What is the syntax for getting an exact image based on its display name from a specific folder using MediaStore ?Cryology
Can also use MediaStore.MediaColumns.RELATIVE_PATH which also is the same path string like question used.Haddix

© 2022 - 2024 — McMap. All rights reserved.