【问题标题】:Is this proper way to download PDF in android?这是在android中下载PDF的正确方法吗?
【发布时间】:2015-09-11 15:31:23
【问题描述】:

我正在使用此代码在 android 中下载和保存 PDF 文件:

HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        HttpResponse response;
        FileOutputStream fileOutputStream;

         /*We will write the file to external storage.
            If External Storage is not available, then we use internal storage
          */
        ApplicationLevel appLevel=(ApplicationLevel) context;
        if(appLevel.isExternalStorageReadable() && appLevel.isExternalStorageWritable())
            file=new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),"example.pdf");
        else
            file=new File(context.getFilesDir(),"example.pdf");


        String path;
        android.util.Log.v(TAG,"strings[0] : "+strings[0]);
        try {
            URI uri = new URI("www.getpdf.com");
            request.setURI(uri);
            response = httpclient.execute(request);
            InputStream in = response.getEntity().getContent();
            String inputLine;

            fileOutputStream = new FileOutputStream(file);
            byte[] buf = new byte[1024];

            android.util.Log.v(TAG, "starting content reading..." );
            while ((in.read(buf)) > -1) {
                fileOutputStream.write(buf);
            }
            android.util.Log.v(TAG,"Content Reading done.");
            in.close();
            fileOutputStream.close();
        } catch (URISyntaxException e) {
            android.util.Log.v(TAG, e.toString());
            return false;
        } catch (IOException e) {
            android.util.Log.v(TAG, e.toString());
            return false;
        }


我认为下载的pdf不合适。当我尝试通过手机上的“AdobeAcrobat”打开 pdf 时,它有时会工作,有时它无法呈现 pdf。
我是否正确下载了 pdf?
这是返回 PDF 的 php 的标头

 header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
               header("Cache-Control: public");
               header("Content-Type: application/pdf");
               header("Content-Transfer-Encoding: Binary");
               header("Content-Length:".filesize($attachment_location));
               header("Content-Disposition: inline; filename=$_GET[get].pdf");

【问题讨论】:

标签: php android


【解决方案1】:

我会这样改变while循环

 int read = 0;
 while ((read = in.read(buf)) > -1) {
      fileOutputStream.write(buf, 0, read);
 }

您不能确保在每次迭代时都准确读取 1024 字节、缓冲区大小,我会添加一个 finally 子句来关闭流:

try {

} catch(..) {

} finally {
  // here call fileOutputStream.close()
  // and in.close()
}

finally 总是被调用,即使是在异常的情况下。所以你不会在出错的情况下泄漏流

我建议您停止使用 Http apache 客户端,而改用 HttpUrlConnection。

【讨论】:

  • 你指的变量buf是什么?
  • byte[] buf = new byte[1024]; ?
  • 最贴切的回答人。这是一直以来的问题。我认为它增加了一些垃圾值,因为我每次都强制写入 1024 个字节。感谢您的回答!一个非常微妙的解决方案。你能说出为什么你更喜欢 HttpURLConnection 而不是 HttpClient 吗?
  • HttpClient 已被弃用,它可能会被 Android M 移除。
  • 酷。无论如何,谢谢你的回答!很好的答案,我相信这是基本层面的
猜你喜欢
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多