【问题标题】:Read picture taken with camera Intent读取用相机拍摄的照片 Intent
【发布时间】:2016-09-21 10:23:49
【问题描述】:

我正在尝试读取并显示使用相机 Intent 拍摄的照片。 我的代码基于 android 文档中的示例:

 public void takeSidePhoto(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile(REQUEST_IMAGE_CAPTURE_SIDE);
        } catch (IOException ex) {

        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this, "my.app.package.fileprovider", photoFile);
            imageSide = photoURI.toString();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_SIDE);
        }
    }
}

private File createImageFile(int imageCaptureCode) throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    return image;
}

问题在于传递给takePictureIntentphotoURI 是(示例): file:/storage/emulated/0/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg 但是当我使用文件管理器浏览我的测试设备存储时,我可以看到拍摄的图片存储在: file:/storage/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg

发生了什么?如何获取真实文件的路径?

【问题讨论】:

  • FileProvider 不会使用来自getUriForFile()file 方案返回Uri 值。它使用content 方案返回Uri 值。除此之外,请准确说明您使用的是什么“文件管理器”。

标签: android file android-intent camera


【解决方案1】:

在你的 try 块中做这样的事情来获取路径

if(photoFile.exists()){
String path = photoFile.getAbsolutePath()
}

【讨论】:

    【解决方案2】:

    takeSidePhoto() 方法应该有以下内容:

      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                    imageUri = Uri.fromFile(photoFile);
                } catch (IOException ex) {
                    Toast.makeText(getActivity(), ex.toString(), Toast.LENGTH_SHORT).show();
                }
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
                }
            } else {
                Toast.makeText(getActivity(), R.string.no_camera_error_message, Toast.LENGTH_SHORT).show();
            }
    

    并在 createImageFile() 方法中更改这一行 File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    用这个File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    【讨论】:

      【解决方案3】:

      使用以下代码获取存储目录并将图像保存到路径。

      String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多