下载WebView中的文件

时间:2014-06-17 22:54:24

标签: java android android-webview

我在我的代码中包含了一个名为'downloadFile'的方法,该方法应该在提供文件时下载文件。然而,这对应用程序没有影响,我想知道我做错了什么。 TIA

MainActivity.java

package com.example.zangle;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    public WebView student_zangle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        WebView student_zangle = (WebView) findViewById(R.id.student_zangle);
        student_zangle.setWebViewClient( new YourWebClient());
        student_zangle.loadUrl("https://zangleweb01.clovisusd.k12.ca.us/studentconnect/");
        student_zangle.setWebViewClient(new WebViewClient());
        student_zangle.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
        WebSettings settings = student_zangle.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);      
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        student_zangle.setDownloadListener(new DownloadListener()
        { 
            public void onDownloadStart(String url, String userAgent, 
                    String contentDisposition, String mimetype, long contentLength)
            { 
                Intent intent = new Intent(Intent.ACTION_VIEW); 
                intent.setType("application/x-rar-compressed"); 
                intent.setData(Uri.parse(url)); 
                startActivity(intent); 

            } 
        }); 
    }

    public void downloadFile(String fileurl, String filename) { 
        String exception; 
        try { 
                FileOutputStream f = new FileOutputStream(filename); 
                try { 
                        URL url = new URL(fileurl); 
                        URLConnection urlConn = url.openConnection(); 
                        InputStream is = urlConn.getInputStream(); 
                        BufferedInputStream bis = new BufferedInputStream(is, 8000); 
                        int current = 0; 
                        while ((current = bis.read()) != -1) { 
                                f.write((byte) current); 
                        } 
                } catch (Exception e) { 
                        exception = e.getMessage(); 
                } 
                f.flush(); 
                f.close(); 
        } catch (FileNotFoundException e) { 
                e.printStackTrace(); 
        } catch (IOException e) { 
                e.printStackTrace(); 
        } 
    } 

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        student_zangle = (WebView) findViewById(R.id.student_zangle);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && student_zangle.canGoBack()) {
            student_zangle.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class YourWebClient extends WebViewClient {     
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("mailto")) {
                String mail = url.replaceFirst("mailto:", "");
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("message/rfc822");
                intent.putExtra(Intent.EXTRA_EMAIL, mail );
                return super.shouldOverrideUrlLoading(view, url);
            } 
            view.loadUrl(url);
            return true;
        }
    }
        public void sendEmail(String email){
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});
            startActivity(Intent.createChooser( emailIntent, "Send mail..."));
        }

}

0 个答案:

没有答案