问题描述:

I've created a webView in my app to view a website. When it comes to browsing webpages, it works just fine. But when I click on a file link(in my webview) that points to file stored on some server e.g. http://www.sgbau.ac.in/revised-engg-tech-w-2014.pdf , it does nothing.

What I intend it to do is download that file (regardless of its extension) into a folder named

"XYZ" on storage.

Here's the code I've tried (in my Java file):

package com.example.jdiet;

import android.os.Bundle;

import android.app.Activity;

import android.content.res.Configuration;

import android.view.KeyEvent;

import android.view.Menu;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public class Sessional extends Activity {

private WebView webView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sessional);

webView = (WebView)findViewById(R.id.webView1);

webView.getProgress();

webView.getSettings().setBuiltInZoomControls(true);

webView.getSettings().setDisplayZoomControls(false);

webView.getSettings().setSupportZoom(true);

webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new MyWebViewClient());

webView.loadUrl("http://www.sgbau.ac.in");

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.sessional, menu);

return true;

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

// Check if the key event was the Back button and if there's history

if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {

webView.goBack();

return true;

}

// If it wasn't the Back key or there's no web page history, bubble up to the default

// system behavior (probably exit the activity)

return super.onKeyDown(keyCode, event);

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

// TODO Auto-generated method stub

super.onConfigurationChanged(newConfig);

}

private class MyWebViewClient extends WebViewClient

{

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

// TODO Auto-generated method stub

return super.shouldOverrideUrlLoading(view, url);

}

}

}

网友答案:

To download a file, you can look at the Android documentation.

The following method extracts the file name from a URL and creates a file with that name in your app's internal cache directory:

public File getTempFile(Context context, String url) {
    File file;
    try {
        String fileName = Uri.parse(url).getLastPathSegment();
        file = File.createTempFile(fileName, null, context.getCacheDir());
    catch (IOException e) {
        // Error while creating file
    }
    return file;
}

If you want to save public files on the external storage, use the getExternalStoragePublicDirectory() method to get a File representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can be logically organized with other public files, such as DIRECTORY_MUSIC or DIRECTORY_PICTURES. For example:

public File getAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}
网友答案:

Enable the DownloadMangager in webview.

mWebView.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);
    }
});

and the browser looks after the downloading.

相关阅读:
Top