[英]How to open pdf files in webview in pdfactivity?
您好,我正在開發應用程序以在 pdfactivity 中打開 webview 中的 pdf 個文件。 我嘗試通過意圖打開它重定向到系統內置應用程序而不是 pdfactivity.java。 找到下面的代碼,我在 webview 活動中聲明了它,
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = WebvActivity.this.getIntent().getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.putExtra(PdfActivity.EXTRA_PDFFILENAME, "url");
startActivity(intent);
return super.shouldOverrideUrlLoading(view, url);
}
我正在使用PdfViewer.jar庫,在 README 文本中提到使用
Intent intent = new Intent(this, WebviewActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");
startActivity(intent);
如何從 webview 中的網頁獲取 URL 個文件中的 URL 個文件並將它們加載到 PdfActivity 中。 請參閱以下鏈接供您參考, https://sourceforge.net/p/andpdf/code/HEAD/tree/tag/Beta_0_1_11/AndroidPdfViewer/activitysrc.net/sf/andpdf/
也許這個鏈接對你很有幫助How to display a PDF via Android web browser without "downloading" first
您可以使用 Google 文檔查看器打開 pdf。
答對了!! 我的問題已解決可以直接使用BufferedInputStream
打開 pdf
我創建了兩個活動,第一個活動加載 webview 中的所有 pdf。當用戶單擊 pdf 的鏈接時, pdfurl
獲取 url 作為字符串,將字符串傳遞給下一個活動。 PdfActivity.java
final String pdfurl = view.getHitTestResult().getExtra();
Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
intent.putExtra("PDFURL",pdfurl);
startActivity(intent);
第二個活動是 PdfViewer,bateksc pdfviewer 從 url 加載 pdf,編輯如上PdfViewer.java
package ak.wp.meto.activity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ak.wp.meto.R;
public class PdfViewer extends Activity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
String value = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("PDFURL");
}
System.out.println("PRINT PDFVIEWER DATA" +value);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = value;
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.