Android7.0调用相机崩溃

2019-05-27  本文已影响0人  南城的人

1、崩溃场景**

2、崩溃的描述

3、解决方案

1、(推荐)7.0之后你的app就算有权限,给出一个URI之后手机也认为你没有权限。
不用修改原有代码,在Application的oncreate方法中:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       StrictMode.VmPolicy.Builder builder = new 
       StrictMode.VmPolicy.Builder();
       StrictMode.setVmPolicy(builder.build());
}

2、在调用相机的时候添加7.0系统的判断然后读取相册

/*获取当前系统的android版本号*/
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Log.e("currentapiVersion","currentapiVersion====>"+currentapiVersion);
if (currentapiVersion<24){
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pathFile));
    startActivityForResult(intent, TAKE_PICTURE);
}else {
    ContentValues contentValues = new ContentValues(1);
    contentValues.put(MediaStore.Images.Media.DATA, pathFile.getAbsolutePath());
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, TAKE_PICTURE);
}

①在mainfest标签里加入以下该权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

②在application下加入该provider

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

在这里我们需要注意一下其中设置的各种属性的含义:

③在res目录下创建xml文件夹,在里面新建filepaths.xml文件,代码如下

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

需要注意的是:

④接下来便是修改适配Android 7.0及以上系统的代码

//拍照
 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //适配android7.0 手机拍照取uri的处理
                if(Build.VERSION.SDK_INT<24){
                    uri = Uri.fromFile(imgFile);//7.0这里会闪退,imgfile是图片文件路径

                    camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                }else{
                    uri=FileProvider.getUriForFile(SellerAffiliate.this,"com.test.fileprovider",imgFile);
                    camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    camera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION );//添加这一句表示对目标应用临时授权该Uri所代表的文件
                }
 startActivityForResult(camera, FLAG_CHOOSE_CAMERA);

//跳转相册
  Intent   intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, FLAG_CHOOSE_IMG);

然后在onactivityresult拍照回调中

if (data != null) {
                String filepath="";
                //拍照返回
                if(getPhotoType.equals("0")){
                    if(uri.getScheme()!=null && "content".equalsIgnoreCase(uri.getScheme())){
                        filepath=imgFile.getAbsolutePath();
                    }else{
                        filepath=uri.getPath();
                    }
                }//相册返回
                else{
                    if (data != null) {
                         filepath =pathStr;
                    }
                }
//                pathStr=path;
                //将图片进行双重压缩后再上传
//                String filepath=BitmapComPressUtils.getRealFilePath(SellerAffiliate.this,uri);
                Bitmap imgBitmap= BitmapComPressUtils.getDecordeImage(SellerAffiliate.this,filepath,800f,480f);
                if(imgBitmap==null){
                    return;
                }
                String imgpath=BitmapComPressUtils.saveBitmap(SellerAffiliate.this,imgBitmap);
                pathStr=imgpath;

上一篇 下一篇

猜你喜欢

热点阅读