2012-05-10 172 views
1

我的路徑的網址是這樣的www.sample.com/sample/1234。 當您單擊路徑上下載一個文件是這樣的從網站下載安卓

Sample_hello_sample.epub 

我不能讓我的應用程序的工作與此下載。

這裏是我的代碼:

private class InsideWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url.contains("sample")) { 
      startDownload(url); 
      return true; 
     } 

     //...? 

     view.loadUrl(url); 
     return true; 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Are you sure you want to exit?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         Gutenbergmain.this.finish(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    private void startDownload(String url) { 
     new DownloadFileAsync().execute(url); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Downloading file.."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 

     default: 
      return null; 
     } 
    } 

    @Override 
    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { 
     if (id == DIALOG_DOWNLOAD_PROGRESS) 
      mProgressDialog.setProgress(0); 
    } 

    class DownloadFileAsync extends AsyncTask<String, String, String> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(DIALOG_DOWNLOAD_PROGRESS); 
      mProgressDialog.setProgress(0); 
     } 

     @Override 
     protected String doInBackground(String... aurl) { 
      int count; 

      try { 
       URL url = new URL(aurl[0]); 

       URLConnection conexion = url.openConnection(); 
       conexion.connect(); 

       int lenghtOfFile = conexion.getContentLength(); 
       Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

       String path = url.getPath(); 
       String idStr = path.substring(path.lastIndexOf('/') + 1); 

       String fileName = idStr; 

       File sdCard = Environment.getExternalStorageDirectory(); 
       File dir = new File (sdCard.getAbsolutePath() + "/Sample/epub"); 
       dir.mkdirs(); 

       File file = new File(dir, fileName); 

       InputStream input = new BufferedInputStream(url.openStream()); 
       FileOutputStream f = new FileOutputStream(file); 

       //InputStream input = new BufferedInputStream(url.openStream()); 
       //OutputStream output = new FileOutputStream("/sdcard/" + fileName); 

       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        publishProgress(""+(int)((total*100)/lenghtOfFile)); 
        f.write(data, 0, count); 
        //output.write(data, 0, count); 
       } 

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

       //output.flush(); 
       //output.close(); 

       input.close(); 
      } catch (Exception e) {} 

      return null; 
     } 

     protected void onProgressUpdate(String... progress) { 
      Log.d("ANDRO_ASYNC",progress[0]); 
      mProgressDialog.setProgress(Integer.parseInt(progress[0])); 

     } 

     @Override 
     protected void onPostExecute(String unused) { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 
    } 
} 

回答

1

必須編輯AndroidManifest.xml文件。例如。

<intent-filter > 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <data android:mimeType="application/*" /> 
    <data android:host="*" /> 
    <data android:pathPattern=".*\\.epub" /> 
</intent-filter> 
+0

從網站上下載的文件名是等於(1234),也就是從這個地址www.sample.com/sample/1234而不是Sample_hello_sample.epub。我覺得有必要在以下字符串這一行做一些修改path = url.getPath(); 。謝謝約翰 –

+0

我現在明白對不起。我會相信你可能想在服務器和你的應用程序中尋找mimeType,但我正在做一個有根據的猜測,但猜測不是那麼簡單。 – John