Captured image was not showing in gallery but it stored successfully in sdcard
Asked Answered
C

2

6

I am new to android development, i am doing small application to capture USB Camrea image using UVCCamera.

I have captured image using UVCCamera and stored in sdcard like this

File filepath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "IMG_"+getDateTime() + ".jpg");
mCameraClient.captureStill(filepath.toString()); 

here mCameraClient will intract with UVC Camera and capture image and store in that path and it will callback method onCaptureDone

@Override
        public void onCaptureDone(final String path) {
            // TODO Auto-generated method stub
            Log.e(TAG, "onCaptureDone  called");
            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    //Toast -- Images Saved.
                }
            });
        }
    };

Then i am invoking gallery from an image button onClickListener

    private MediaScannerConnection conn;

File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
    File[] mImagefile = folder.listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String filename) {

                            return filename.endsWith(".jpg");
                    }
                });

                if (mImagefile.length > 0) {

                    SCAN_PATH = folder.toString() + "/"
                            + mImagefile[mImagefile.length -1].getName();
                    Log.e(TAG, "SCAN PATH = " + SCAN_PATH);

                    if (conn != null) {
                        conn.disconnect();
                    }

                    conn = new MediaScannerConnection(this, this);
                    conn.connect();
                } else {
                    //Toast -- No Images Available
                }

If filter successful.

    @Override
    public void onMediaScannerConnected() {
        // TODO Auto-generated method stub
        Log.e(TAG, "onMediaScannerConnected");
        ;
        conn.scanFile(SCAN_PATH, null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        // TODO Auto-generated method stub
        try {
            Log.e(TAG, "OnCompleted  path = " + path);
            if (uri != null) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(uri);
                startActivity(intent);
            }
        } finally {
            Log.e(TAG, "OnCompleted Disconnecting");
            conn.disconnect();
            conn = null;
        }
    }

Here my problem was in gallery all captured images was not displaying.

And i have checked in saved directory, there all images was stored successfully.[Images was not corrupted, i have verified]

And i have checked in default gallery application, in that also those images was not displaying.

Please help me on this.

Edit 1:

I have observed one thing, when i capture and saved image, it not showing in gallery but after some time like 2-3Hrs its was appearing in gallery. I WANT TO KNOW WHY LIKE THIS HAPPENING?

Thanks

Commodore answered 3/8, 2015 at 16:35 Comment(2)
You have to notify the media scanner about the new image. Refer #4753752Brindisi
@HarishSridharan : I have tried, not workingCommodore
U
9

Put this line of code after capture an image and save it to sd card.

File image_path="your image path";
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(image_path)));

Here image_path is saved image file's path

Hope this helps you..!

Uncivil answered 6/8, 2015 at 15:37 Comment(0)
B
1

You have to insert into media gallery and create a thumbnail for it. Otherwise until media gallery does the scan (usually scheduled like scan for new images once in two hours) you won't seeing them in gallery.

Use the following to make an entry and create a thumbnail to media gallery.

    String imageFilename = "IMG_";
    String originalPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
    String path = "";


    File filepath = new File(originalPath + imageFilename+".jpg");
    mCameraClient.captureStill(filepath.toString());


    try {
        newPath = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), originalPath, imageFilename, "Captuted Image");
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
Brindisi answered 6/8, 2015 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.