問(wèn)題:android 7.0手機(jī)打開(kāi)相機(jī)出現(xiàn)崩潰绽快?
android.os.FileUriExposedException:file:///storage/emulated/0/Pictures/IMG_20170315065632.jpg expos
解決方案:
- targetSdkVersion寫(xiě)低于24的形病;
- 使用ContentProvider:http://blog.csdn.net/huangxiaoguo1/article/details/52830015 (搜索到的解決問(wèn)題的博客)
- 還有一種使用FileProvider:http://www.cnblogs.com/netcorner/p/6542373.html
http://blog.csdn.net/honjane/article/details/52057132
(網(wǎng)上很多,直接貼了某個(gè)解決的網(wǎng)址了)
使用ContentProvider方式具體解決代碼如下:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(context.getPackageManager()) != null) {
/*獲取當(dāng)前系統(tǒng)的android版本號(hào)*/
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, "相機(jī)不可用", Toast.LENGTH_SHORT).show();
}
最近看到的好的文章: