cannot set a correct content-type for downloaded pdf in android
I cannot open a pdf that i download from the web:
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. I set the path to where to be downloaded, i open an url connection and set the contenty type like this:
URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/pdf"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Disposition","attachment; filename=" + filename); 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.connect();
But when i read what type of content type i have:
System.out.println("Connection type : " + connection.getContentType());
the answer is: text/html
... :(
And of course i cannot open the pdf with adobe, i get and error saying "Unable to open this file". I think it's because the content type is not correct.
Does anyone got into this error before? I looked at this p开发者_如何学Goost but still does not work... Thank you for your time.
The type you set for the Intent has nothing to do with the content type set in the URLConnection object.
精彩评论