[英]cannot set a correct content-type for downloaded pdf in android
I cannot open a pdf that i download from the web: 我无法打开从网上下载的pdf文件:
In first activity class i set the intent type like this: 在第一个活动类中,我将意图类型设置如下:
> Intent myIntent = new Intent(getApplicationContext(),SecondClassActivity.class);
myIntent.setType("application/pdf");
I get to the second class activity that has the logic for downloading the pdf from an URL. 我进入第二类活动,该活动具有从URL下载pdf的逻辑。 I set the path to where to be downloaded, i open an url connection and set the contenty type like this:
我设置了要下载的路径,我打开了一个url连接并设置了如下内容类型:
URLConnection connection = url.openConnection();
URLConnection连接= url.openConnection(); connection.setDoOutput(true);
connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/pdf");
connection.setRequestProperty(“ Content-Type”,“ application / pdf”); connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty(“ Connection”,“保持活动”); connection.setRequestProperty("Content-Disposition","attachment; filename=" + filename);
connection.setRequestProperty(“ Content-Disposition”,“ attachment; filename =” + filename); connection.setRequestProperty("charset", "UTF-8");
connection.setRequestProperty(“ charset”,“ UTF-8”); connection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.13) Gecko/2009073021 Firefox/3.0.13");
connection.setRequestProperty(“ User-Agent”,“ Mozilla / 5.0(Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.13)Gecko / 2009073021 Firefox / 3.0.13”); connection.connect();
connection.connect();
But when i read what type of content type i have: 但是当我阅读什么类型的内容类型时,我会:
System.out.println("Connection type : " + connection.getContentType());
the answer is: text/html
... :( 答案是:
text/html
... :(
And of course i cannot open the pdf with adobe, i get and error saying "Unable to open this file". 当然,我无法使用Adobe打开pdf,但出现错误并提示“无法打开此文件”。 I think it's because the content type is not correct.
我认为这是因为内容类型不正确。
Does anyone got into this error before? 有人遇到过这个错误吗? I looked at this post but still does not work... Thank you for your time.
我看了这篇文章,但仍然无法正常工作...谢谢您的宝贵时间。
您为Intent设置的类型与URLConnection对象中设置的内容类型无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.