Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 453
iosLearner Avatar asked Aug 03 '15 16:08

iosLearner


1 Answers

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..!

like image 81
Jenisha Makadiya Avatar answered Sep 28 '22 08:09

Jenisha Makadiya