2

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);
                }
            }
Mood Board
  • 59
  • 1
  • 7
  • You cannot query the MediaStore for a specific folder. The only thing you can do is query a MediaStore collection for files. Use the same collection as during save inspect RELATIVE_PATH if it is the wanted folder. – blackapps Aug 02 '20 at 11:58

0 Answers0