简体   繁体   中英

How to open pdf files in webview in pdfactivity?

Hello I'm developing app to open pdf files in webview in pdfactivity. I tried opening through intent its redirecting to system inbuilt apps but not pdfactivity.java. find the code below, I declared this in my webview activity,

 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);
        }

I'm using PdfViewer.jar library, In README text it is mentioned use

Intent intent = new Intent(this, WebviewActivity.class);
     intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");
     startActivity(intent);

How can I get URL of pdf files from the webpage in webview & load them in PdfActivity. See below link for your reference, https://sourceforge.net/p/andpdf/code/HEAD/tree/tag/Beta_0_1_11/AndroidPdfViewer/activitysrc.net/sf/andpdf/

Maybe this link could be very helpful for you How to display a PDF via Android web browser without "downloading" first

You can open a pdf using Google Docs Viewer.

BINGO!! My issue resolved can open pdf directly using BufferedInputStream

I created two activity, First Activity to load all pdfs in webview. When user clicks the link of pdf, the pdfurl fetches the url as strings, passes strings to next activity. PdfActivity.java

   final String pdfurl = view.getHitTestResult().getExtra();
         Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
         intent.putExtra("PDFURL",pdfurl);
         startActivity(intent);

Second Activity is PdfViewer, bateksc pdfviewer to load pdf from url, edited as above 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();
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM