代码之家  ›  专栏  ›  技术社区  ›  luke88

照片后如何更新Android Gallery?

  •  5
  • luke88  · 技术社区  · 11 年前

    为了大学,我必须在安卓系统上开发一个人脸检测应用程序。 为此,我必须在我的图库中保存各种照片。 问题是保存照片后,图库不会更新。 更确切地说,如果我删除了要保存图像的目录,我会打开应用程序并拍摄照片,然后进入画廊,“更新”后我会看到照片。 但一旦我有了目录,如果我拍了一张新照片,这并没有覆盖旧的。

    我在网上找到了这个:

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

    …但它不起作用!

    这是我的代码:

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

    希望你能帮忙。 再见

    1 回复  |  直到 11 年前
        1
  •  10
  •   etienne    11 年前

    您可以强制MediaScanner添加特定文件(或让它知道它已被删除或修改)

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