Captured image was not showing in gallery but it s

2019-02-14 05:35发布

问题:

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

回答1:

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



回答2:

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();
    }