2012-05-10 9 views
1

私のパスのURLは、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

ウェブサイトからダウンロードしたファイル名は、このアドレスwww.sample.com/sample/1234からではなくSample_hello_sample.epub .IがString以下、この行に必要ないくつかの変更があると思います(1234年)に等しく、 path = url.getPath(); 。ありがとうJohn –

+0

私は今、ごめんなさいと理解しています。私はあなたがサーバとあなたのアプリケーションでmimeTypeをlookintoするのが好きかもしれないと信じていましたが、私は教育的な推測をしていますが、推測は少ないです。 – John

関連する問題