How can I update the Android Gallery after a photo

2019-01-12 00:56发布

问题:

For the university i've to develop an application on android with the face detections. For this i've to save various photo on my gallery. The problem is that after saving the photo, the gallery do not update. More precisely, if I delete the directory where I'm going to save the image, I open the application and shoot the photo, then going into the gallery and after an "update" I see the photo. But once I have the directory, if I take a new picture, this did not overwrite the old one.

Online i've found this:

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));

...but it doesn't work!

This is my code:

    .
    .
    .
      ImageButton buttonPicture = (ImageButton) findViewById(R.id.camera_surface_button);
                        buttonPicture.setOnClickListener(new OnClickListener(){
                                public void onClick(View v) {
                                        mCamera.takePicture(null, null, jpegCallback); 
                                        //File file = new File(savedPath, "ing.jpg");
                                        sendBroadcast(new         Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" +     Environment.getExternalStorageDirectory())));

                                }
                        });
     .
     .
     .
     .

    PictureCallback jpegCallback = new PictureCallback() {
                public void onPictureTaken(byte[] _data, Camera _camera) {
                        imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/TTRprova");
                        imagesFolder.mkdirs();
                        String fileName = "image3.jpg";

                        File output = new File(imagesFolder, fileName);

                        textView1.setText("-----Sono nella callback-----");

                        Uri outputFileUri = Uri.fromFile(output);
                        String filePath = outputFileUri.getPath();
                        File file= new File(filePath);

                        try {
                            FileOutputStream fos = new FileOutputStream(file, true);
                            fos.write(_data);
                            fos.close();

                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            textView1.setText("-----FileNotFoundException-----");
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            textView1.setText("-----IOException-----");
                            e.printStackTrace();
                        }

                        //riparte la preview della camera
                        //mCamera.startPreview();



                }
    };

Hope in your help. Bye

回答1:

You can force the MediaScanner to add a specific file (or let it know it was deleted or modified)

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));