大约2天后,我尝试在自定义WebvView
中打开PDF文件。这是我的WebView
代码:
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebActivity extends AppCompatActivity {
public
WebView mywebViewfull;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
String url = getIntent().getStringExtra("url");
mywebViewfull = (WebView) findViewById(R.id.webnav1);
progressBar=(ProgressBar)findViewById(R.id.progressbar1);
WebSettings webSettings= mywebViewfull.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setGeolocationEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSaveFormData(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(false);
webSettings.setGeolocationDatabasePath(getFilesDir().getPath());
webSettings.setAllowFileAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
mywebViewfull.loadUrl(url);
mywebViewfull.setWebViewClient(new mywebViewfullClient());
}
@Override
public void onBackPressed() {
if(mywebViewfull.canGoBack())
{
mywebViewfull.goBack();
}
else
{
super.onBackPressed();
}
}
class mywebViewfullClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// android.support.v7.app.ActionBar actionBar = getSupportActionBar();
// actionBar.hide();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://")) {
//open url contents in webview
return false;
}if (url != null && url.startsWith("https://")) {
//open url contents in webview
return false;
//this must open pdf file
}if (url.endsWith(".pdf")) {
mywebViewfull.loadUrl("https://docs.google.com/gview?embedded=true&url="+getIntent().getStringExtra("url"));
return false;
}
else {
//external links in app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
//back previous page
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
if (mywebViewfull.canGoBack()) {
mywebViewfull.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
所以...当我尝试打开pdf document时,我的应用中没有任何反应。
我看不出有什么问题!任何人都可以帮助我吗?
答案 0 :(得分:9)
在Android中打开PDF文件没有本机支持,但是您可以使用一些或多或少可以为您完成工作的解决方法。备选方案:
1 - 您可以打开系统默认应用,通过应用中的意图阅读pdf文件;
2 - 您可以使用第三方库在您的应用内打开pdf;
3 - 您可以调用Google Docs网站功能在您的网页浏览中打开您的pdf文件。为此,请使用以下代码示例:
String pdfUrl = "http://yourwebsite.com/yourfile.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
答案 1 :(得分:1)
如果您选择
,请尝试以下网页选项WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "online pdf link";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
但请记住,如果您使用Google文档查看pdf或任何其他文档,Google文档可能会向您显示错误说明
You've reached the bandwidth limit for viewing or downloading files that aren't in Google Docs format
.....所以看起来不可靠
因此,请谨慎使用Google doc查看
如果您有通过资产文件夹阅读的PDF文件,请参阅我的whole answer
答案 2 :(得分:-1)
如果要在Web视图中显示PDF,请使用:
url = "https://docs.google.com/viewer?url=https://linkToYourPdf.pdf"
webView.loadUrl(url);
如果您的PDF包含一页以上的内容,请不要忘记启用垂直滚动
webview.setVerticalScrollBarEnabled(true);