[英]When I click on a link in my webview that points to a file, nothing happens
我已经在应用程序中创建了一个webView来查看网站。 在浏览网页时,它可以正常工作。 但是,当我单击文件链接(在我的Web视图中)时,该文件链接指向存储在某些服务器上的文件,例如http://www.sgbau.ac.in/revised-engg-tech-w-2014.pdf ,则它什么都不做。
我打算将文件(无论其扩展名)下载到存储上名为“ XYZ”的文件夹中。
这是我尝试过的代码(在Java文件中):
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);
}
}
}
要下载文件,您可以查看Android文档 。
以下方法从URL中提取文件名,并在应用程序的内部缓存目录中创建一个具有该名称的文件:
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;
}
如果要将公共文件保存在外部存储器上,请使用getExternalStoragePublicDirectory()方法来获取一个代表外部存储器上适当目录的File。 该方法采用一个参数,该参数指定要保存的文件的类型,以便可以与其他公共文件(例如DIRECTORY_MUSIC或DIRECTORY_PICTURES)进行逻辑组织。 例如:
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;
}
在网络视图中启用DownloadMangager。
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);
}
});
然后浏览器会负责下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.