2017-05-16 4 views
1

J'ai url charge dans WebView en application, mais la fonctionnalité de téléchargement ne fonctionne pas dans WebView click sur rien bouton de téléchargement est arrivé s'il vous plaît me donner la solution.J'ai url charge dans WebView mais la fonctionnalité de téléchargement qui ne travaillent pas dans WebView quand cliquez sur le bouton de téléchargement

Public class FinancialActivity extends AppCompatActivity { 

WebView webview; 
public static final String SHARED_PREF_NAME = "myloginapp"; 
SharedPreferences sharedPreferences; 
String id, studid, degree, user_type; 
String url; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_financial); 

    sharedPreferences = FinancialActivity.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
    studid = sharedPreferences.getString(LoginActivity.ID_SHARED_PREF, "Not Available"); 
    degree = sharedPreferences.getString(LoginActivity.KEY_DEGREE_SOUGHT, "Not Available"); 
    user_type = sharedPreferences.getString("user_type", "Not Available"); 

    url = Urlinfo.Financial + "stud_id=" + studid + "&user_type=" + user_type + "&subscription_status=" + DrawerActivity.subvalue + "&stud_degree=" + degree; 

    Log.d("urlapplication", "" + url); 

    WebView w = new WebView(this); 
    w.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    view.loadUrl(url); 
    return true; 
    } 
    }); 

    w.setDownloadListener(new DownloadListener() { 
    public void onDownloadStart(String url, String userAgent, 
    String contentDisposition, String mimetype, 
    long contentLength) { 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setData(Uri.parse(url)); 
    startActivity(i); 
    } 
    }); 

    setContentView(w); 
    w.loadUrl(url);  
} 
} 
} 
+0

vous voulez télécharger des fichiers à partir de l'URL qui est ouvert dans WebView ?? –

+0

oui ............. –

+0

vérifier la réponse ci-dessous –

Répondre

0

essayer ici le code ci-dessous

@Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        // handle different requests for different type of files 
        // this example handles downloads requests for .apk and .mp3 files 
        // everything else the webview can handle normally 
        if (url.endsWith(".apk")) { //change .apk to any format you want to download 
         Uri source = Uri.parse(url); 
         // Make a new request pointing to the .apk url 
         DownloadManager.Request request = new DownloadManager.Request(source); 
         // appears the same in Notification bar while downloading 
         request.setDescription("Description for the DownloadManager Bar"); 
         request.setTitle("YourApp.apk"); 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
          request.allowScanningByMediaScanner(); 
          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
         } 
         // save the file in the "Downloads" folder of SDCARD 
         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk"); 
         // get download service and enqueue file 
         DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
         manager.enqueue(request); 
        } 
        else if(url.endsWith(".mp3")) { 
         // if the link points to an .mp3 resource do something else 
        } 
        // if there is a link to anything else than .apk or .mp3 load the URL in the webview 
        else view.loadUrl(url); 
        return true;     
      } 

Hope this helps