CameraX: Make loading picture in gallery spontaneous

183 Views Asked by At

I am developing an android app based on CameraX. I have successfully designed the (image) preview part of the app with some loading animation and now have also added an functionality to view the picture in gallery along with other pictures taken by the app.

The issue here is sometimes the latest picture doesn't show up in the gallery, just as it shows up in the preview. I have used MediaScanner.scanFiles and it even runs the success callback. But it randomly takes a while to load the picture in gallery. Based on my observations, it loads the first picture quickly, but then takes a while to load an alternate picture (which again loads quickly). I tried scanning the same file twice, but it doesn't work as expected. Maybe I could scan the file, rename/(delete+create) it and again scan it, but would be too much of a workaround with no guarantee that the file would be immediately visible as soon as I open the gallery. Am I doing something wrong or missing out on something?

Here's the repository: https://github.com/MHShetty/grapheneos-camera

(Also, scanning the parent file tends to make it more spontaneous, but it isn't true always; it is slower with scanning the exact file on my device)

Code that executes after the image gets saved

                        Log.i(TAG, "Image saved successfully!");
                        final Uri imageUri = outputFileResults.getSavedUri();
                        if(imageUri!=null){
                            final String path = imageUri.getEncodedPath();
                            final Bitmap bm = BitmapFactory.decodeFile(path);
                            final File file = new File(path);

                            mActivity.getConfig().setLatestFile(file);

                            final String mimeType = MimeTypeMap.getSingleton()
                                    .getMimeTypeFromExtension(
                                            CamConfig.getExtension(new File(path))
                                    );

                            MediaScannerConnection.scanFile(
                                    mActivity,
                                    new String[]{file.getParent()},
                                    new String[]{mimeType},
                                    (path1, uri) -> {
                                        Log.d(TAG, "Image capture scanned" +
                                                " into media store: " + uri);

                                        mActivity.runOnUiThread(()-> {
                                            mActivity.getPreviewLoader()
                                                    .setVisibility(View.GONE);

                                            if (bm != null)
                                                mActivity.getImagePreview()
                                                        .setImageBitmap(bm);

                                        });
                                    }
                            );
                        }

Code to open gallery

    public void openGallery() {

        String mediaId = "";

        String[] projection = new String[] {
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DISPLAY_NAME
        };

        Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, null, null, null);

        if(cursor != null){
            while (cursor.moveToNext()) {
                String name = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)));
                if(name.equals(config.getLatestMediaFile().getName())){
                    mediaId = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
                    break;
                }
            }
            cursor.close();
        }

        Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        if(!mediaId.equals("")){
            mediaUri = mediaUri.buildUpon()
                    .authority("media")
                    .appendPath(mediaId)
                    .build();
        }

        Log.d("TagInfo","Uri:  "+mediaUri);

        Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
        startActivity(intent);
    }

Thanks a lot in advance.

0

There are 0 best solutions below