如何在拍照后更新Android相册?

5

我需要开发一款运用人脸检测技术的Android应用程序。为此,我需要在我的相册中保存多张照片。问题是,在保存照片后,相册没有更新。更具体地说,如果我删除将要保存图像的目录,然后打开应用程序并拍摄照片,然后进入相册,并进行“更新”,我就会看到照片。但是,一旦我有了目录,如果我拍摄新照片,则不会覆盖旧照片。

我在网上找到了这个问题的解决方案:

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



                }
    };

希望你能帮助我。 再见。

你使用的是什么设备? - FoamyGuy
这个问题肯定是一个重复的,这个问题已经被问过很多次了。所以我相信你贴出来的那一行代码应该是正确的答案。你的问题可能与你的清单文件有关,可能是缺少权限、缺少活动/广播标签或类似的问题。你是在模拟器上运行还是在实体设备上运行? - Tobrun
请查看此链接:https://dev59.com/9mYr5IYBdhLWcg3w8-gz - Numair
1个回答

10

您可以强制让媒体扫描器添加特定的文件(或让它知道该文件已被删除或修改)

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

Xamarin Context.SendBroadcast(new Intent(Intent.ActionMediaScannerScanFile, Android.Net.Uri.FromFile(YourJavaFile))); Xamarin 上下文发送广播(new Intent(Intent.ActionMediaScannerScanFile, Android.Net.Uri.FromFile(YourJavaFile))); - Nick Kovalsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接