2012-05-09 79 views
3

我想在下載PDF文件時從網站檢索原始文件名,而不是將其重命名,而是將其保存爲SD/Android/Data目錄下的同名文件/(MYAPP)/文件。截至目前,我必須告訴android在查找文件時的文件名以及下載時保存的文件名。有沒有一種簡單的方法來改變它以將其保存爲原始名稱?由於Android - 獲取異步下載的原始文件名

private void startDownload() { 

    String isFileThere = Environment.getExternalStorageDirectory() 
      + "/Android/Data/" + getApplicationContext().getPackageName() 
      + "/files/xxxxxxxx.pdf"; 
    File f = new File(isFileThere); 

    if (f.exists()) { 
     mProgressDialog.setProgress(0); 
     showPdf(); 
    } else { 

     DownloadFile downloadFile = new DownloadFile(); 
     downloadFile 
       .execute(url); 

    } 
} 

class DownloadFile extends AsyncTask<String, Integer, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressDialog.show(); 
    } 

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

    @Override 
    protected String doInBackground(String... aurl) { 

     try { 

      URL url = new URL(aurl[0]); 
      URLConnection connection = url.openConnection(); 

      connection.connect(); 
      int fileLength = connection.getContentLength(); 
      int tickSize = 2 * fileLength/100; 
      int nextProgress = tickSize; 

      Log.d(

      "ANDRO_ASYNC", "Lenght of file: " + fileLength); 

      InputStream input = new BufferedInputStream(url.openStream()); 

      String path = Environment.getExternalStorageDirectory() 
        + "/Android/Data/" 
        + getApplicationContext().getPackageName() + "/files"; 
      File file = new File(path); 
      file.mkdirs(); 
      File outputFile = new File(file, "xxxxxxxx.pdf"); 

      OutputStream output = new FileOutputStream(outputFile); 

      byte data[] = new byte[1024 * 1024]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       if (total >= nextProgress) { 
        nextProgress = (int) ((total/tickSize + 1) * tickSize); 
        this.publishProgress((int) (total * 100/fileLength)); 
       } 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
      mProgressDialog.setProgress(0); 
      mProgressDialog.dismiss(); 

     } catch (Exception e) { 
     } 
     return null; 
    } 

    { 

    } 

    @Override 
    protected void onPostExecute(String unused) { 

     File file = new File(Environment.getExternalStorageDirectory() 
       + "/Android/Data/" 
       + getApplicationContext().getPackageName() 
       + "/files/" + "xxxxxxxx.pdf"); 
     Intent testIntent = new Intent(Intent.ACTION_VIEW); 
     testIntent.setType("application/pdf"); 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri uri = Uri.fromFile(file); 
     intent.setDataAndType(uri, "application/pdf"); 
     try { 
      startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
      Toast.makeText(atcChoice.this, 
        "No PDF viewer is available, please download one from Play store", 
        Toast.LENGTH_LONG).show(); 

     } 
    } 

} 
+0

也許一個愚蠢的queestion,但不包含在URL中的文件名?喜歡:http://www.blabla.com/my_pdf.pdf – Entreco

+0

我的問題是,我總是指定文件名,不知道要更改哪部分,以允許它下載原始文件名並保存爲該名稱。你知道我應該調整以實現這個目標嗎? – user1363871

回答

8

使用URLUtil.guessFileName()

這裏是如何使用它的一個簡單的例子嘗試:

String url = http://...; 
String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl(url); 
String name = URLUtil.guessFileName(url, null, fileExtenstion); 
+0

你能告訴我如何調整這段代碼來使用URLUtil.guessFileName()嗎?我嘗試過自己,但它不能正常工作。 – user1363871

+0

我已經添加了一個如何使用它的簡單示例 – avimak