android 7.0拍照崩溃适配

方法一:
    这个是为了获取uri  7.0认为直接访问本地的uri是不安全的 需要通过Uri uri=getUriForFile(context,"任意字符串",file)\
    
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.ok.shipments.Activity.GoodsContorlActivity"//这个随意 不过要对应
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
file_paths  在res目录下新建xml文件夹即可

//file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path=""/>
</paths>
代码
myCaptureFile = new File(mFilePath);//mfilePth就是你要保存的图片路径
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
LogU.e("currentapiVersion", "currentapiVersion====>" + currentapiVersion);
if (currentapiVersion<24){
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myCaptureFile));
startActivityForResult(intent, SELECT_CAMER);
}else {
    Uri uri=FileProvider.getUriForFile(this, "com.example.ok.shipments.Activity.GoodsContorlActivity",myCaptureFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, SELECT_CAMER);
}


方法二:
要给照片设置一个Uri 防止返回的data为null
方法二不用配置其他东西 直接使用即可

myCaptureFile = new File(mFilePath);//mfilePth就是你要保存的图片路径
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
LogU.e("currentapiVersion", "currentapiVersion====>" + currentapiVersion);
if (currentapiVersion<24){
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myCaptureFile));
startActivityForResult(intent, SELECT_CAMER);
}else {
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, myCaptureFile.getAbsolutePath());
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, SELECT_CAMER);
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值