表示路径在应用中红内部存储区域中cache目录下的子目录下,cache-path表示Context.getCacheDir()
的根目录。
例如:/data/data/com.bugly.upgrade.demo/cache
表示路径在外部存储区域根目录的子目录,external-path表示Environment.getExternalStorageDirectory()
的根目录。
例如:/storage/emulated/0
表示路径在外部存储区域根目录的缓存目录,external-cache-path表示Context.getExternalCacheDir()
。
例如:/storage/emulated/0/Android/data/com.bugly.upgrade.demo/cache
- 通过FileProvider获取Uri路径
示例代码:
Uri uri = Uri.fromFile(file);
可以更改为:
Uri uri = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + “.fileProvider”, file);
因为我们SDK不会引入support-v4包,所以不能通过上面这种方式直接获取uri,最后考虑通过反射来调用getUriForFile方法,具体实现如下:
Intent i = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 反射获取FileProvider类型
Class<?> clazz = Class.forName(“android.support.v4.content.FileProvider”);
if (c
lazz == null) {
ELog.error(“can’t find class android.support.v4.content.FileProvider”);
return false;
}
// 通过反射调用FileProvider.getUriForFile
Uri contentUri = (Uri) Utils.invokeReflectMethod(“android.support.v4.content.FileProvider”, “getUriForFile”
, null, new Class[]{Context.class, String.class, File.class},
new Object[]{context, ComInfoManager.getCommonInfo(context).boundID + “.fileProvider”, file});
if (contentUri == null) {
ELog.error("file location is " + file.toString());
ELog.error(“install failed, contentUri is null!”);
return false;
}
i.setDataAndType(contentUri, “application/vnd.android.package-archive”);
} else {
i.setDataAndType(Uri.fromFile(file), “application/vnd.android.package-archive”);
}
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
调用反射方法定义如下:
public static Object invokeReflectMethod(String className, String methodName, Object instance,
Class<?>[] paramTypes, Object[] params) {
try {
Class<?> clazz = Class.forName(className);
Method method = clazz.getDeclaredMethod(methodName, paramTypes);
method.setAccessible(true);
return method.invoke(instance, params);
} catch (Exception e) {
return null;
}
}
大致的解决方案就如上所示啦,已经在Android 7.0验证通过了,由于在Android 7.0以上强制要求配置FileProvider,但考虑API低于24以下还是沿用之前的方法,所以只在API高于24才会去使用FileProvider。
总结
==
关于Android N共享文件权限的适配已经完成,还有其他特性还需要我们去验证看是否存在一些bug,其实Android每一个版本的发布都会面临这样一个问题,所以我们去了解每个版本特性的变化还是很有必要的,每次更新targetSdkVersion的时候,最好的实践就是根据特性变化列一个checklist来进行适配。好了,本篇文章内容就这么多,可能有讲得不清楚的地方,请见谅。
参考资料
====
http://www.jianshu.com/p/3f9e3fc38eae
欢迎关注我的公众号:wwjblog
-share-access-to-file-with-fileprovider-on-android-nougat/en]( )
http://www.jianshu.com/p/3f9e3fc38eae
欢迎关注我的公众号:wwjblog