2012-12-10 15 views
5

おはようございます、 私はアプリを作っていますが、私は壁に当たっています。 firefoxやchromeのようなデスクトップブラウザを使用してWebサイトにアクセスしてダウンロードリンクをクリックするとファイルをダウンロードし始めますが、webviewを使用してページを表示してダウンロードリンクをクリックすると、まったく起こりません。そのリンクで何をすべきか分からないように、ただそこに座っています。あなたが私を助けることができれば、それは大きな助けとなり、私を再び軌道に乗せさせるでしょう。webviewはファイルをダウンロードしません

これは私が私のサイト上でPDFファイルに使用ダウン​​ロードリンクです:

<img src="images/Art1.jpg" width="80" height="166" /><a href="m2.pdf">Download</a> 

は、これは私のAndroidアプリのための私のmain.xmlファイルです:

<?xml version="1.0" encoding="utf-8"?> 
    <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    /> 

私はほとんどになりますしてください誰かが私を正しい道に向けるのを助けることができれば、感謝しています。

package com.jeffonlinelibrary; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class WebPageLoader extends Activity 
{ 
    final Activity activity = this; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     setContentView(R.layout.main); 
     WebView webView = (WebView) findViewById(R.id.webview); 
     webView.getSettings().setJavaScriptEnabled(true); 

     webView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) 
      { 
       activity.setTitle("Loading..."); 
       activity.setProgress(progress * 100); 

       if(progress == 100) 
        activity.setTitle(R.string.app_name); 
      } 
     }); 

     webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
      { 
       // Handle the error 
      } 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       view.loadUrl(url); 
       return true; 
      } 
     }); 

     webView.setDownloadListener(new DownloadListener() { 

      public void onDownloadStart(String url, String userAgent, 
        String contentDisposition, String mimetype, 
        long contentLength) { 
       // handle download, here we use brower to download, also you can try other approach. 
       Uri uri = Uri.parse(url); 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 

     webView.loadUrl("http://jeffonlinelibrary.comuv.com/jeffOnlinelibraryApp"); 
    } 
} 

答えて

8

DownloadListenerをWebViewに設定します。ブラウザーアプリまたはDownloadManagerを使用してファイルをダウンロードします。

webview.setDownloadListener(new DownloadListener() { 
     @Override 
     public void onDownloadStart(String url, String userAgent, 
       String contentDisposition, String mimetype, 
       long contentLength) { 
      // handle download, here we use brower to download, also you can try other approach. 
      Uri uri = Uri.parse(url); 
      Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
      startActivity(intent); 
     } 
    }); 
+0

この回答をお寄せいただきありがとうございますが、少し混乱しています。私はこのコードを入力する必要がありますか? – chinedudeoracle

+0

...あなたのアクティビティでwebviewを見つけ、それにDownloadListenerを設定します。 – faylon

+0

もう一度すべてのヘルプをありがとうございますが、私のアクティビティファイルにコードを挿入すると、次のようないくつかのエラーが生成されました: – chinedudeoracle

関連する問題