gpt4 book ai didi

java - Android - 在 webview 中加载 PDF

转载 作者:太空狗 更新时间:2023-10-29 15:32:53 25 4
gpt4 key购买 nike

我有这个 webview 代码,我希望能够在用户单击 PDF 链接时打开 PDF 文件。这是代码,你能告诉我我必须在这个 PDF 区域内放什么吗?我尝试了很多不同的方法,但根本无法查看 PDF。感谢您的帮助。

webview.setWebViewClient ( new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
if (url.startsWith("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
} else if (url.startsWith("mailto:")) {
url = url.replaceFirst("mailto:", "");
url = url.trim();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL,
new String[]{url});
startActivity(i);

} else if (url.startsWith("geo:")) {
try {
} catch (Exception e) {
System.out.println(e);
}

} else if (url.endsWith("pdf")) {

try {

} catch (Exception e) {
System.out.println(e);
}

} else {
view.loadUrl(url);
}
return true;
// then it is not handled by default action
}
});

最佳答案

这可以很简单:

try
{
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(url, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(myActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}

虽然我没试过这个。

在我们的应用程序中,我们将 PDF 下载到应用程序文件系统,使其可读,然后在 Intent 中传递路径以打开 PDF 查看应用程序(例如 Acrobat Reader)。请注意,您还需要关注清理这些下载的 PDF!

在你的try block 中放置

new DownloadPDFTask().execute(url);

下载PDF任务类:

public class DownloadPDFTask extends AsyncTask<String, Void, Integer> 
{
protected ProgressDialog mWorkingDialog; // progress dialog
protected String mFileName; // downloaded file
protected String mError; // for errors

@Override
protected Integer doInBackground(String... urls)
{

try
{
byte[] dataBuffer = new byte[4096];
int nRead = 0;

// set local filename to last part of URL
String[] strURLParts = urls[0].split("/");
if (strURLParts.length > 0)
mFileName = strURLParts[strURLParts.length - 1];
else
mFileName = "REPORT.pdf";

// download URL and store to strFileName

// connection to url
java.net.URL urlReport = new java.net.URL(urls[0]);
URLConnection urlConn = urlReport.openConnection();
InputStream streamInput = urlConn.getInputStream();
BufferedInputStream bufferedStreamInput = new BufferedInputStream(streamInput);
FileOutputStream outputStream = myActivity.openFileOutput(mFileName,Context.MODE_WORLD_READABLE); // must be world readable so external Intent can open!
while ((nRead = bufferedStreamInput.read(dataBuffer)) > 0)
outputStream.write(dataBuffer, 0, nRead);
streamInput.close();
outputStream.close();
}
catch (Exception e)
{
Log.e("myApp", e.getMessage());
mError = e.getMessage();
return (1);
}

return (0);
}

//-------------------------------------------------------------------------
// PreExecute - UI thread setup
//-------------------------------------------------------------------------

@Override
protected void onPreExecute()
{
// show "Downloading, Please Wait" dialog
mWorkingDialog = ProgressDialog.show(myActivity, "", "Downloading PDF Document, Please Wait...", true);
return;
}

//-------------------------------------------------------------------------
// PostExecute - UI thread finish
//-------------------------------------------------------------------------

@Override
protected void onPostExecute (Integer result)
{
if (mWorkingDialog != null)
{
mWorkingDialog.dismiss();
mWorkingDialog = null;
}

switch (result)
{
case 0: // a URL

// Intent to view download PDF
Uri uri = Uri.fromFile(myActivity.getFileStreamPath(mFileName));

try
{
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(uri, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(myActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}

break;

case 1: // Error

Toast.makeText(myActivity, mError, Toast.LENGTH_LONG).show();
break;

}

}

}

任何对“myActivity”的引用都必须替换为对您的 Activity 类的引用

关于java - Android - 在 webview 中加载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10520009/

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