android7.0以上调用相机
生成的文件路径跟指定的path不匹配问题( mFile = new File(getExternalCacheDir(), "images/"+System.currentTimeMillis() + ".jpg"); 与 <external-cache-path name="hm_external_cache" path="images" /> )
getExternalCacheDir()生成的路径为
/storage/emulated/0/Android/data/com.ruhai.yibei/cache/
完整代码如下
File mFile;
Uri mImageUri;
private void openCamera() {
//创建文件用于保存图片
mFile = new File(getExternalCacheDir(), "images/"+System.currentTimeMillis() + ".jpg");
if (!mFile.exists()) {
try {
mFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//适配7.0
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mImageUri = Uri.fromFile(mFile);
} else {
//第二个参数要和清单文件中的配置保持一致
mImageUri = FileProvider.getUriForFile(PersonInfoActivity.this, "com.ruhai.yibei.provider", mFile);
}
<?xml version="1.0" encoding="utf-8"?> <paths> <external-cache-path name="hm_external_cache" path="images" /> </paths>