美文网首页@IT·互联网程序员Android 文章
Android 7.0拍照获取相片报错原因

Android 7.0拍照获取相片报错原因

作者: flemingchen | 来源:发表于2017-04-27 14:43 被阅读254次

前言

应用中调用系统相机拍照,并希望获取全尺寸图片,这本是很简单常用的功能,不过在Android 7.0的设备上竟然却崩溃了,查了一下原因,在这里记录一下。

哪里出错,报什么错

要实现上述功能,之前常见的代码是这样:

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String fileName = DateFormat.format("yyyyMMdd_HHmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
            filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + fileName;
            // 将filepath作为Uri对象传给照相机
            Uri photoUri = Uri.fromFile(new File(filePath));
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
}

应用跑在Android 7.0手机上发生崩溃,
报错发生在:Uri photoUri = Uri.fromFile(new File(filePath));
报错类型:android.os.FileUriExposedException

原因

当应用程序暴露了一个文件file://给另一个应用,而这在7.0版本上是不允许的,因为接收应用程序可能没有请求READ_EXTERNAL_STORAGE运行时权限,或者平台可能在用户配置文件边界之间共享Uri。

解决方案

应该用content://的方式,这样就可以扩展接收应用程序的临时权限来访问资源。也就是给应用增加一个content provider,指定到保存图片的路径。

第一步 创建file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="images_path" path="Pictures" />
</paths>

在paths节点内部支持以下几个子节点,分别为:

  • <root-path/> 代表设备的根目录new File("/");
  • <files-path/> 代表context.getFilesDir()
  • <cache-path/> 代表context.getCacheDir()
  • <external-path/> 代表Environment.getExternalStorageDirectory()
  • <external-files-path>代表context.getExternalFilesDirs()
  • <external-cache-path>代表getExternalCacheDirs()

第二步 在AndroidManifest.xml文件中创建<provider>标签

<provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="${applicationId}.fileprovider"
      android:exported="false" //不可修改,其他进程不可访问
      android:grantUriPermissions="true"> //不可修改,必须给访问权限
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" />
</provider>

第三步 修改代码

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = createImageFile();
            Uri photoUri = FileProvider
                    .getUriForFile(this, this.getPackageName() + ".fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    private File createImageFile() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINESE).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); //这里就是Pictures,图片的实际下载目录
        File image = null;
        try {
            image = File.createTempFile(imageFileName, ".jpg", storageDir);
            mCurrentPhotoPath = image.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

相关文章

网友评论

    本文标题:Android 7.0拍照获取相片报错原因

    本文链接:https://www.haomeiwen.com/subject/bppgzttx.html