Android 7.0 拍照 FileUriExposedException

最近開發(fā)項目遇到7.0 拍照崩潰記錄此問題

這個異常只會在Android 7.0+ 上會出現(xiàn)此問題,當app使用file:// url 共享給其他app時, 會拋出這個異常
官方推薦使用FileProvider 來解決此問題

  • 第一步在manifest.xml文件添加provider,相機,讀寫文件權限

  • 第二步在appliction 節(jié)點中插入代碼,注意 android:authorities 里面的值,在后面使用的getUriForFile(Context, String, File) 中, 第二個參數(shù)就是這個里面的值,請務必填寫正確。

    <provider
         android:name="android.support.v4.content.FileProvider"
         android:authorities="com.example.android.fileprovider"
         android:exported="false"
         android:grantUriPermissions="true">
         <meta-data
             android:name="android.support.FILE_PROVIDER_PATHS"
             android:resource="@xml/provider_paths"></meta-data>
      </provider>
    
-  第三步在res節(jié)點下創(chuàng)建xml包并添加provider_paths.xml文件

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


paths 可配置選項

<files-path name="name" path="path" /> //相當 Context.getFilesDir() + path, name是分享url的一部分

<cache-path name="name" path="path" /> //getCacheDir()

<external-path name="name" path="path" /> //Environment.getExternalStorageDirectory()

<external-files-path name="name" path="path" />//getExternalFilesDir(String) Context.getExternalFilesDir(null)

<external-cache-path name="name" path="path" /> //Context.getExternalCacheDir()


-  第4步在activity中調用拍照版本高于或等于android 7.0使用FileProvider.getUriForFile 否則使用Uri.fromFile(file);

/**
* 打開系統(tǒng)相機
*/
private void openCamera() {
File file = new FileStorage().createIconFile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//通過FileProvider創(chuàng)建一個content類型的Uri ,和清單文件保持一致
imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", file);
} else {
imageUri = Uri.fromFile(file);
}
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對目標應用臨時授權該Uri所代表的文件
}
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設置Action為拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片保存到指定URI
startActivityForResult(intent, REQUEST_CAPTURE);
}

public File createIconFile() {
String fileName = "";
if (iconDir != null) {
fileName = UUID.randomUUID().toString() + ".png";
}
return new File(iconDir, fileName);
}

-  File file = new FileStorage().createIconFile(); 
創(chuàng)建文件

public class FileStorage {
private File cropIconDir;
private File iconDir;

public FileStorage() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File external = Environment.getExternalStorageDirectory();
        String rootDir = "/" + "image";
        cropIconDir = new File(external, rootDir + "/crop");
        if (!cropIconDir.exists()) {
            cropIconDir.mkdirs();

        }
        iconDir = new File(external, rootDir + "/icon");
        if (!iconDir.exists()) {
            iconDir.mkdirs();

        }
    }
}

public File createCropFile() {
    String fileName = "";
    if (cropIconDir != null) {
        fileName = UUID.randomUUID().toString() + ".png";
    }
    return new File(cropIconDir, fileName);
}

public File createIconFile() {
    String fileName = "";
    if (iconDir != null) {
        fileName = UUID.randomUUID().toString() + ".png";
    }
    return new File(iconDir, fileName);
}

}


-  裁剪

/**
* 裁剪
/
private void cropPhoto() {
File file = new FileStorage().createCropFile();
Uri outputUri = Uri.fromFile(file);//縮略圖保存地址
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(imageUri, "image/
");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, REQUEST_PICTURE_CUT);
}



- 第5步 activity 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//此處為系統(tǒng)拍照截圖返回
cameraProxy.onResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CAPTURE://拍照
if (resultCode == RESULT_OK) {
cropPhoto();
}
break;
case REQUEST_PICTURE_CUT://裁剪完成
spUtil.setUserHead(imageUri.getPath());
break;

    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容