昨天一朋友遇到bug,解决不了了,于是本着你有困难我帮忙的心态帮他看一下~。
要做的功能是将app的信息保存在手机.txt文件,然后调用html查看器等工具进行查看。但调用的时候一直不成功。
参数传入的true,我稍微调整下,传入false,模拟器(5.1.1)可以顺利打开进行查看。但他的小米手机(API 28)打不开,看了一下代码,他没有动态申请读写权限,保存日志到手机需要读写权限,于是手动加上requestPermiss,运行之,遇到第二个bug
FileProvider 也就是7.0以后加入的临时权限,这个也需要适配,比如拍照
或者读写文件等。
那么就加上吧~
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(context,"com.example.myapplication.fileProvider",file);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
}else{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri2 = Uri.fromFile(file);
intent.setDataAndType(uri2, "text/plain");
}
乍一看,应该就可以了,想着分分钟帮他解决bug暗自窃喜。。。
但运行的时候懵逼了。。。
出现了 包解析异常,噫,正常来讲应该直接打开.txt日志文件了,咋还包解析呢,又不是安装包。。。 等等,安装包?
《 application/vnd.android.package-archive》
这是隐士打开的类型,这个类型是打开apk的。除此之外 还有别的。。。
final String[][] MIME_MapTable={
//{后缀名,MIME类型}
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
{"", "*/*"}
};
{".txt", "text/plain"}, 对应是text/plain。
于是稍加修改:
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(context,"com.example.myapplication.fileProvider",file);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri,"text/plain");
}else{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri2 = Uri.fromFile(file);
intent.setDataAndType(uri2, "text/plain");
}
运行 完美解决。后续我会封装一个工具类,用于打开各种文件~
总结:代码千万条,细心第一条。