简体   繁体   English

无法为Android中下载的pdf设置正确的内容类型

[英]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.

相关问题 Android WebView:当内容类型为 application/pdf 时下载,如果不是则呈现网页 - Android WebView: Download when content-type is application/pdf and render web page if not Multipart,设置一部分的 Content-Type - Multipart, set Content-Type of one part 使用OkHTTP进行改造未使用@FormUrlEncoded设置Content-Type - Retrofit with OkHTTP not set Content-Type with @FormUrlEncoded 如何使用杰克逊库(此宁静的.net Web API)在android中设置标头和内容类型 - How to set headers and content-type in android using jackson library (this restful .net web api) 邮件附件没有正确的Content-Type - Mail attachment does not have correct Content-Type Android Pixel 2发送了错误的Content-Type或内容 - Android Pixel 2 sending incorrect Content-Type or content 无法在OkHttp中为MultipartBody.Part设置Content-Type - Can't set Content-Type for MultipartBody.Part in OkHttp Android AsyncHttpClient下载包含Content-Type:unkonwn标头的图像文件 - Android AsyncHttpClient download image file with Content-Type: unkonwn header Android通过MultipartEntity将图像发送到服务器 - 设置内容类型? - Android sending image to Server via MultipartEntity - setting Content-type? 带有身份验证的Android Java http删除Content-Type - Android java http with authentication remove Content-Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM