gpt4 book ai didi

android - 在 WebView 中打开 PDF 文件

转载 作者:太空狗 更新时间:2023-10-29 15:37:38 26 4
gpt4 key购买 nike

大约 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 时,我的应用程序没有任何反应。

我看不出有什么问题!谁能帮帮我?

最佳答案

Android 不支持打开 PDF 文件,但您有一些解决方法或多或少可以为您完成这项工作。备选方案:

1 - 您可以打开系统默认应用程序以通过应用程序的 Intent 读取 pdf 文件;

2 - 您可以使用第三方库在您的应用程序中打开 pdf;

3 - 您可以调用 Google Docs 网站功能在您的 WebView 中打开您的 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);

关于android - 在 WebView 中打开 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48792702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com