Android 7.0拍照崩溃问题

问题:android 7.0手机打开相机出现崩溃?
android.os.FileUriExposedException:file:///storage/emulated/0/Pictures/IMG_20170315065632.jpg expos

解决方案:
  • targetSdkVersion写低于24的;
  • 使用ContentProvider:http://blog.csdn.net/huangxiaoguo1/article/details/52830015 (搜索到的解决问题的博客)
  • 还有一种使用FileProvider:http://www.cnblogs.com/netcorner/p/6542373.html
    http://blog.csdn.net/honjane/article/details/52057132
    (网上很多,直接贴了某个解决的网址了)

使用ContentProvider方式具体解决代码如下:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(context.getPackageManager()) != null) {
        /*获取当前系统的android版本号*/
      int currentApiVersion = Build.VERSION.SDK_INT;
      if (currentApiVersion < Build.VERSION_CODES.N) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            context.startActivityForResult(intent, REQUEST_CAMERA);
       } else {
            ContentValues contentValues = new ContentValues(1);
            contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
            Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            context.startActivityForResult(intent, REQUEST_CAMERA);
        }
} else {
    Toast.makeText(context, "相机不可用", Toast.LENGTH_SHORT).show();
}

最近看到的好的文章:

  • https://youzanmobile.github.io/2017/04/01/fileprovider-on-n/

你可能感兴趣的:(Android 7.0拍照崩溃问题)