android 7.0调用相机闪退问题

xiaoxiao2021-02-28  30

解决方案一: 一般归类为手机兼容性问题,要么是软件系统兼容导致,要么是硬件配置过低导致。 具体实现的代码:

String photoNameAll = PhotoUtil.getphotoName();// 获取照片名字 String state = Environment.getExternalStorageState();//检测内存卡 int currentapiVersion = android.os.Build.VERSION.SDK_INT;//获取版本 File tempFile = new File(PictureShowUtils.getDirName(), photoNameAll + ".jpg");//文件名称 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//激活相机 if (Environment.MEDIA_MOUNTED.equals(state)) { if (currentapiVersion < Build.VERSION_CODES.N) { //从文件中创建uri intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); //startActivityForResult(intent, 0); } else { //兼容android7.0 使用共享文件的形式 ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.Images.Media.DATA, tempFile.getAbsolutePath()); Uri uri = getApplication().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA startActivityForResult(intent, 0); } else { Toast.makeText(this,"请检查内存卡", 0).show(); }

解决方案二: Android7.0的禁止不安全路径被外部访问。 具体实现代码:

//置入一个不设防的VmPolicy if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); }
转载请注明原文地址: https://www.6miu.com/read-2400275.html

最新回复(0)