6.0版本及以前查看sd卡图片的intent:
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "image/*");
7.0之后,安全性提高了不少,面向 Android 7.0 或更高版本的应用私有目录被限制访问 (0700)。此设置可防止私有文件的元数据泄漏,如它们的大小或存在性.
bug1: 安卓7.0 FileUriExposeException 跳转相机页面的bug
解决:FileProvider.getUriForFile 替换 Uri.fromFile
bug2: 点击后 出现raw image not detected 的log
解决:添加一个intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
给访问赋予权限。
本来赋予的权限已经够多了。 读写sd卡 以及这个uri的获取。真没有想到还需要这么一个权限 。
网上查了好多都没有找到。后来看了7.0权限的一些东西。尝试着添加了这个权限。没想到就真的成了。yes!!!
希望能够帮助到有相同困扰的朋友。
源码:
public static Intent getImageFileIntent(Context context,String param) {
Uri uri;
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//判断安卓版本是否是7.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
uri = FileProvider.getUriForFile(context, MyApp.getContext().getPackageName() + ".provider", new File(param));
//安卓版本7.0以上需要再次给一个这个权限。
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {//7.0
uri = Uri.fromFile(new File(param));
}
intent.setDataAndType(uri, "image/*");
return intent;
}