【问题标题】:How can I update the Android Gallery after a photo?照片后如何更新 Android 图库?
【发布时间】:2013-03-28 01:53:01
【问题描述】:

对于大学,我必须在 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();



                }
    };

希望得到您的帮助。 再见

【问题讨论】:

  • 你用的是什么设备?
  • 这个问题肯定是重复的,这个问题已经被问过很多次了。所以我相信你发布的那条线将是正确的答案。您的问题可能与您的清单、缺少权限或缺少活动/广播标签或类似内容有关。你是在模拟器还是物理设备上运行它?

标签: android android-intent gallery photo face-detection


【解决方案1】:

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

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

【讨论】:

  • Xamarin Context.SendBroadcast(new Intent(Intent.ActionMediaScannerScanFile, Android.Net.Uri.FromFile(YourJavaFile)));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多