我写了一个小型的Android应用程序,该应用程序具有指向网络上PDF文件的链接。问题是,该链接是没有适当文件后缀的通用链接。然而,提供文件的网络服务器将发送一个具有适当后缀的真实文件名,并强制网页浏览器将文件保存在一个不错的名字下(文件下载很常见)。 这适用于FF或IE等任何桌面浏览器,但如果我在Android上启动VIEW Intent,它会在原始文件下启动下载,导致无后缀且与任何程序无关的文件。Android - 通过URL打开PDF文件,通过HTTP头文件设置通过意图
(ADOBE READER安装并手动下载改名打开就好了)
例子: 链接到“http://mysample.com/file/6dbfj73bdngdn3”将被标题改为“mysamplefile.pdf”
这里是PHP设置下载文件的标题:
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: private, must-revalidate, post-check=0, pre-check=0");
header("Content-length: 12345");
header("Content-type: application/force-download; filename=\"mysamplefile.pdf\"");
header("Content-type: application/octet-stream; filename=\"mysamplefile.pdf\"");
header("Content-type: application/download; filename=\"mysamplefile.pdf\"");
我试过几种方法来打开这个打算,这是我目前的一个。如果我指定一个MIME类型,我会得到一个ActivityNotFoundException,如果我不这样做,我会在没有后缀的原始文件名下得到上述提到的下载。
String url = "http://mysample.com/file/6dbfj73bdngdn3";
//Intent i = new Intent(Intent.ACTION_VIEW);
//i.setDataAndType(Uri.parse(url), "application/pdf");
Intent i = new Intent(Intent.ACTION_VIEW, URI.parse(url));
看来,Android的忽略了一些与这些标题是否包含在任何文件名或参数之间,或者分号在结尾处。 – ToBe 2013-02-27 10:43:54