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

时间:2011-07-30 18:48:49

标签: android pdf types

我无法打开从网上下载的pdf:

在第一个活动类中,我设置了这样的意图类型:

> Intent myIntent = new Intent(getApplicationContext(),SecondClassActivity.class);
myIntent.setType("application/pdf");

我进入了第二类活动,它具有从URL下载pdf的逻辑。 我设置了下载位置的路径,我打开了一个url连接并设置了这样的contenty类型:

  

URLConnection connection = url.openConnection();         connection.setDoOutput(真);         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();

但是当我读到我所拥有的内容类型时:

System.out.println("Connection type : " + connection.getContentType()); 答案是:text/html ...... :(

当然我无法用adobe打开pdf,我得到并且错误地说“无法打开此文件”。我认为这是因为内容类型不正确。

之前有没有人遇到此错误?我看了这个post,但仍然无效......谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

您为Intent设置的类型与URLConnection对象中设置的内容类型无关。