【问题标题】:Images not being saved when picture is taken by camera app that isn't the stock camera由不是库存相机的相机应用程序拍摄照片时未保存图像
【发布时间】:2014-08-11 01:59:09
【问题描述】:

我目前正在尝试将从手机拍摄的图像保存到其图库中,但只有在弹出选择器对话框时选择库存相机应用程序时,以下代码才有效。每当我选择其他相机应用(例如 Google 相机)时,拍摄的照片都不会保存在任何地方。

为了让事情变得更奇怪,有时图片确实会出现在画廊的指定目录中,但在 15 分钟左右后,当我使用股票相机应用程序时也是如此:图片将保存在默认值中相机镜头目录,但需要相当长的时间才能显示在指定的目录中,如果它显示在那里的话。

// Capturing Camera Image will launch camera app request image capture
void captureImage() {
    //file uri to store image.
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    // Request camera app to capture image
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    getActivity().startActivityForResult(captureIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

【问题讨论】:

    标签: android image camera gallery android-gallery


    【解决方案1】:

    嗯,
    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri); 不再起作用了。 你应该做这样的事情: 调用相机活动:

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    

    和 onActivityResult :

     if (data.getData() == null) {  
        Bitmap bm = (Bitmap)
      data.getExtras().get("data");
       String timeStamp = new SimpleDateFormat(
                                "yyyyMMdd_HHmmss").format(new Date());
    
      File pictureFile = new File(Environment
                                .getExternalStoragePublicDirectory(
                                        Environment.DIRECTORY_PICTURES)
                                .getAbsolutePath()
                                + File.separator + "IMG_" + timeStamp);
    
    try {
         FileOutputStream fos = new FileOutputStream(
                                    pictureFile);
        bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.close();
         String filePath = pictureFile.getAbsolutePath();
       } catch (FileNotFoundException e) {
        e.printStackTrace();
       } catch (IOException e) {
       e.printStackTrace();
       }   } else {
    
       Uri imgUri =data.getData());  
    

    }

    【讨论】:

    • 您好,感谢您的帮助。这段代码对我不起作用。它基本上是在做我在原始代码中已经做过的事情,只是有点不同。结果是一样的。阅读我上面的答案。我的问题是我没有更新图库。
    【解决方案2】:

    事实证明我的代码毕竟是有效的。照片被保存在新目录中,但问题是图库没有更新,这解释了为什么照片会在以后随机出现在目录中。作为新手,我从来没有想过我必须更新画廊。我是在使用 ES 文件资源管理器查看我的文件后才意识到这一点的。为了解决我的问题,我刚刚在我的 CameraFragment 中创建了一个可以调用媒体扫描仪的新方法。我从 onActivityResult() 中调用了这个方法。

    这是新方法,虽然没有什么真正的“新”,因为我在其他 SO 问题上遇到了相同的代码:

    protected void mediaScan() {
        getActivity().sendBroadcast(
                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
                        Uri.parse(fileUri.toString())));
    }
    

    如果我没有提供使用从图库中选择图片的选项,我也不需要调用包管理器并遍历可以处理相机意图的应用程序,因此我将删除所有这是我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      相关资源
      最近更新 更多