[英]why does Uri.fromFile(new File(url)) work and Uri.parse(url) not in open pdf by intent?
下面的代码可以按意图打开pdf文件,效果很好:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(url)), "application/pdf");
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
但是如果我将Uri.fromFile(new File(url))
更改为Uri.parse(url)
,如下所示,它将无法打开pdf文件。 为什么?
intent.setDataAndType(Uri.parse(url), "application/pdf");
我知道两种方法返回的实例的类类型不同,但这是上面代码起作用的关键吗?
顺便说一句,URL是正确的并且该文件存在。
Uri是用于向ContentProvider提供内容的Type。 uri是pdf文件的路径吗? Uri.parse怎么知道您要传递的是pdf而不是URL? 这就是为什么需要fromFile的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.