android 7相机拍照功能介绍,Android7.0调用相机拍照问题

Android7.0后用调用拍照直接崩溃,报FileUriExposedException异常,以后Android7.0调用拍照就需要这样处理了。

public static void takePhoto(final Context context) {

File file = new File(IMAGE_PATH);

if (!file.exists() || !file.isDirectory()) {

file.mkdirs();

}

File outFile = new File(IMAGE_PATH, "image.jpg");

if (outFile.exists()) {

outFile.delete();

}

try {

outFile.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (Build.VERSION.SDK_INT >= 24) {

intent.putExtra(MediaStore.EXTRA_OUTPUT,

FileProvider.getUriForFile(context, "com.goldou.ygty.fileprovider", outFile));

} else {

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile));

}

((Activity) context).startActivityForResult(intent, 102);

}

AndroidManifest.xml

添加provider,authorities的值是包名+.fileprovider

android:name="android.support.v4.content.FileProvider"

android:authorities="com.goldou.ygty.fileprovider"

android:grantUriPermissions="true"

android:exported="false">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths" />

file_paths.xml

这里不加root-path的话会报IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Ygty/image.jpg异常,加上后正常运行。

name="camera_photos"

path="eg:/storage/emulated/0/Ygty" />

name="root_path"

path="." />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值