android 7.0 因为file://引起的FileUriExposedException异常

释放双眼,带上耳机,听听看~!
 private void dialog() {
SheetDialog sheetDialog = new SheetDialog(this);
sheetDialog.setItemClickListener(new SheetDialog.OnSheetDialogListener() {
@Override
public void onItemClick(int id, int position) {
switch (id) {
case SheetDialog.TYPE_TAKE_PICTURE:
if (PermissionUtils.lacksPermissions(ChatReportActivity.this,
PERMISSIONS)) {
// 缺少权限时, 进入权限配置页面
PermissionsActivity.startActivityForResult(ChatReportActivity.this,
REQUEST_CODE_ASK_CAMERA_PERMISSION, PERMISSIONS);
} else {
showCameraAction();
}
break;
case SheetDialog.TYPE_SELECT_PHOTO:
if (PermissionUtils.lacksPermissions(ChatReportActivity.this,
PERMISSIONS)) {
// 缺少权限时, 进入权限配置页面
PermissionsActivity.startActivityForResult(ChatReportActivity.this,
REQUEST_CODE_ASK_WRITE_PERMISSION, PERMISSIONS_WRITE);
} else {
goToSelectPicture();
}
break;
}
}
});
sheetDialog.show();
}

调用相机:

private void showCameraAction() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
mTmpFile = FileUtils.createTmpFile(this);
} catch (IOException e) {
e.printStackTrace();
}
Uri photoOutputUri = FileProvider.getUriForFile(this,
getPackageName() + ".fileprovider", mTmpFile);
if (mTmpFile != null && mTmpFile.exists()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoOutputUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
}

调用相册:

private void goToSelectPicture() {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "选择图片"), REQUEST_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 拒绝时, 关闭页面, 缺少主要权限, 无法运行
if (requestCode == REQUEST_CODE_ASK_CAMERA_PERMISSION) {
if (resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
Toast.makeText(ChatReportActivity.this, "缺少必要权限", Toast.LENGTH_SHORT).show();
} else {
showCameraAction();
}
return;
}
if (resultCode == RESULT_OK) {
handleImgRequest(requestCode, data);
}
}

配置文件需要加入:

      <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yjtx.fsl.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

然后在,res目录中,新建一个xml文件,然后创建 file_paths.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--<external-path path="Android/data/com.yjtx.fsl/" name="files_root" />-->
<!--<external-path path="." name="external_storage_root" />-->
<external-path name="external_files" path="."/>
</paths>

 

人已赞赏
Android文章

Android 中全局更换字体的方法

2020-4-20 12:18:16

Android文章

Android Junit Test(semc-build编译并且生成emma coverage)

2020-4-20 13:33:15

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索