android - 非库存相机的相机应用拍摄照片时未保存图像

标签 android image camera gallery android-gallery

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

更奇怪的是,有时图片确实会出现在图库的指定目录中,但 15 分钟左右后,当我使用股票相机应用程序时也是如此:图片将保存在默认目录中camera shots 目录,但要显示在其指定目录中需要相当长的时间,如果它完全显示在那里的话。

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

}

最佳答案

嗯,
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 不再工作了。 你应该这样做: 调用相机 Activity :

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

关于android - 非库存相机的相机应用拍摄照片时未保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234897/

相关文章:

iOS - 是否可以在将图像文件从应用程序保存到 iPhone 设备库之前重命名图像文件名?

ios - ios中如何知道相机拍照完成

android - 在android中为不同版本编程不同的布局

java - 如何从 Python 中的套接字读取原始字节?

android - 将odex文件转换为dex文件

javascript - 检查图像是否以 jQuery(或纯 JavaScript)加载

Android:将应用与自定义文件类型相关联

image - wordpress 是否应该通过 https 自动加载图像?

html - CSS - 保持高度为 100% 的图像比例,而其宽度会迫使它变形?

c# - Unity C# : Camera Control Using Automatic Positioning & Mouse Look