Bugly升级SDK适配Android N,跨平台移动应用框架

表示路径在应用中红内部存储区域中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

  1. 通过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来进行适配。好了,本篇文章内容就这么多,可能有讲得不清楚的地方,请见谅。

参考资料

====

https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en

http://www.jianshu.com/p/3f9e3fc38eae


欢迎关注我的公众号:wwjblog
这里写图片描述
-share-access-to-file-with-fileprovider-on-android-nougat/en]( )

http://www.jianshu.com/p/3f9e3fc38eae


欢迎关注我的公众号:wwjblog
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值