[英]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.