【问题标题】:Android camera crop not supporting in Nougat 7Nougat 7 不支持 Android 相机裁剪
【发布时间】:2017-10-19 04:24:19
【问题描述】:

在 android nougat 7 中打开相机时,android nougat 7 不支持相机和图库裁剪我收到此错误消息。

android.os.FileUriExposedException:

file:///storage/emulated/0/file1495176310055.jpg 暴露在应用程序之外 通过 ClipData.Item.getUri()

【问题讨论】:

  • 这不是裁剪问题。 Android 7 中的文件访问系统发生了变化。您必须从现在开始实现您的内容提供程序,以使其他应用程序能够使用您提供的 Uris
  • exposed beyond app through ClipData.Item.getUri() 我想知道这怎么可能。

标签: android camera crop android-7.0-nougat


【解决方案1】:

对于面向 Android 7.0 的应用,Android 框架强制执行 StrictMode API 政策,该政策禁止在您的应用之外公开 file:// URI。如果包含文件 URI 的 Intent 离开您的应用,应用将失败并出现 FileUriExposedException 异常。

要在应用程序之间共享文件,您应该发送一个 content:// URI 并授予对 URI 的临时访问权限。授予此权限的最简单方法是使用 FileProvider 类。

你可以试试我的解决方案..

1.添加res/xml/provider_paths.xml

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

2.在Manifest里面添加标签

     <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="android3.maxtingapp.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

3.在您的活动中添加裁剪图像的功能,如下所示

private void cropImage(File file) {
       final int width  = 400;
       final int height = 200;

   try {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");

    Uri contentUri;

        if(Build.VERSION.SDK_INT > M){

             contentUri = FileProvider.getUriForFile(AddPlace.this,
                                   "android3.maxtingapp.provider",
                                    file);//package.provider

            //TODO:  Permission.. 

            getApplicationContext().grantUriPermission("com.android.camera",
                                                         contentUri,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        }else{

            contentUri = Uri.fromFile(file);

        }

        cropIntent.setDataAndType(contentUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 2);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", width);
        cropIntent.putExtra("outputY", height);

        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, REQUEST_CROP_ICON);

    }catch (ActivityNotFoundException a) {
        Log.e("Activity Not Found",""+a.toString());
    }
}

我希望这对某人有所帮助..

【讨论】:

  • 嗨@Dinesh,上面的代码适用于上面的牛轧糖吗?我得到了类似 Editing is not supported for this image 之类的弹出窗口,我这边有什么错误吗..?
猜你喜欢
  • 2013-07-01
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 2013-05-26
相关资源
最近更新 更多