2012-10-29 92 views
2

我从聊天中获取.pdf文件,我想要下载它并使用acrobat reader将其显示出来。以下是我的代码Android下载并从url中显示pdf

public void showPDF(String pdf) 
     { 
      try 
      { 
       Log.i("pic", " * * * in showPdf" + pdf); 

       String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
       File folder = new File(extStorageDirectory, "pdf"); 
       folder.mkdir(); 
       String pdf_name = pdf.replace(".pdf", ""); 
       pdf_name = pdf_name.replace("/fileupload/data/chat/", ""); 
       File file = new File(folder, pdf_name + ".pdf"); 
       try 
       { 
        file.createNewFile(); 
       } 
       catch (IOException e1) 
       { 
        e1.printStackTrace(); 
       } 
       Log.i("pic", " * * * ready to download"); 
       new DownloadFile(file, Functions.getServiceProtocol() + Functions.getServiceDomain() + pdf, pdf_name).execute(); 
      } 
      catch (Exception e) 
      { 
       Log.i("pic", " CATCH * * * in showPdf"); 
      } 
     } 
     } 



    private class DownloadFile extends AsyncTask<String, Integer, String> 
     { 
      private String fileURL, pdfname; 
      private File directory; 
      private ProgressDialog dlg = new ProgressDialog(CameraWebview.this); 

      public DownloadFile(File d, String f, String n) 
      { 
       directory = d; 
       fileURL = f; 
       pdfname = n; 
      } 

      @Override 
      protected String doInBackground(String... sUrl) 
      { 
       try 
       { 
        Log.i("pic", " TRY * * * download file"); 
        FileOutputStream f = new FileOutputStream(directory); 
        URL u = new URL(fileURL); 
        HttpURLConnection c = (HttpURLConnection)u.openConnection(); 
        c.setRequestMethod("GET"); 
        c.setDoOutput(true); 
        c.connect(); 

        InputStream in = c.getInputStream(); 

        byte[] buffer = new byte[1024]; 
        long total = 0; 
        int count; 

        while ((count = in.read(buffer)) != -1) { 
         total += count; 
         f.write(buffer, 0, count); 
        } 

        f.flush(); 
        f.close(); 

        try 
        { 
         Log.i("pic", " TRY * * * calling displayPdf"); 
         displayPdf(pdfname); 
        } 
        catch(Exception ex) 
        { 
         Log.i("pic", " CATCH * * * calling displayPdf"); 
        } 
       } 
       catch (Exception e) 
       { 
       } 
       return null; 
      } 

      @Override 
      protected void onPreExecute() 
      { 
       super.onPreExecute(); 
       dlg.setMessage("Downloading File ..."); 
       dlg.show(); 
      } 

      @Override 
      protected void onProgressUpdate(Integer... progress) 
      { 
       super.onProgressUpdate(progress); 
       mProgressDialog.setProgress(progress[0]); 
      } 

      @Override 
      protected void onPostExecute(String result) 
      { 
       super.onPostExecute(result); 
       if (dlg.isShowing()) 
        dlg.dismiss(); 
      } 
     } 

     public void displayPdf(String pdf_name) 
     { 
      try 
      { 
       Log.i("pic", " TRY * * * ready to show pdf"); 
       File file = new File(Environment.getExternalStorageDirectory() + "/pdf/" + pdf_name + ".pdf"); 
       PackageManager packageManager = getPackageManager(); 
       Intent testIntent = new Intent(Intent.ACTION_VIEW); 
       testIntent.setType("application/pdf"); 
       List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); 
       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_VIEW); 
       Uri uri = Uri.fromFile(file); 
       intent.setDataAndType(uri, "application/pdf"); 
       startActivity(intent); 
       Log.i("pic", " TRY * * * here is the pdf"); 
      } 
      catch (Exception e) 
      { 
       Log.i("pic", " CATCH * * * show pdf file" + e.toString()); 
      } 

它总是说“损坏的文件”。我检查了我用它的网址,这很好。另外我的应用程序是冰淇淋Sandwitch。我究竟做错了什么 ?

+0

我不明白你为什么要在displayPdf中创建一个新的File对象。为什么不使用现有的文件,因为它是你首先编写pdf的地方? – njzk2

+0

另外,你看过你的设备中的文件? – njzk2

+0

是的,这是alwya 20KB损坏文件 – user200658

回答

1

当您拨打DownloadFile(File d, String f, String n)时,您如何获取目录?

如果您使用getExternalStorageDirectory(我假设您是),那么pdf对于您的应用程序是私人的,并且不能被其他应用程序访问。

如果您需要该文件的私人存到自己的应用程序,并且仍然可以使用其他应用程序打开,使用ContentProvider

如果你能保持在一个共享目录中的文件,你应该使用getExternalStoragePublicDirectory()获得保存文件的路径,以及稍后检索文件URI。

此问题与this之一有关,因此类似的答案。

这应该解决的问题:

showPDF

String extStorageDirectory = Environment.getExternalStoragePublicDirectory(
     Environment.DIRECTORY_DOWNLOADS).toString(); 

displayPdf

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/pdf/" + pdf_name + ".pdf"); 
+0

我编辑了代码 – user200658

+0

我编辑了我的答案,看看它是否有帮助 – Marcelo

+0

好。让我试试看。 – user200658

1

您正在使用setRequestMethod后setDoOutput(真)( “GET”)。

setDoOutput(true)将您的请求方法自动设置为POST。

0
     theurl = new URL(url);      
         InputStream in = null; 
         File dst = new File(Environment.getExternalStorageDirectory()+"/file.pdf"); 
          try { 
           in = theurl.openStream(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          }    
          OutputStream out = null; 
         try { 
          out = new FileOutputStream(dst); 
         } catch (FileNotFoundException e) { 
          // TODO Auto-generated catch block 
         } 
          // Transfer bytes from in to out 
          try { 
           byte[] buf = new byte[100000]; 
           int len; 
          while ((len = in.read(buf)) > 0) { 
           out.write(buf, 0, len); 
          } 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
          try { 
          in.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
          try { 
          out.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         }