DImuthuUpe / AndroidPdfViewer

Android view for displaying PDFs rendered with PdfiumAndroid
Apache License 2.0
8.05k stars 1.85k forks source link

Unable to view PDF From Amazon Service Url in Android . #1026

Open rituapplocum opened 2 years ago

SoengSaravit commented 2 years ago

Try this code:

InputStream inputStream = null;
// TODO: Retrieve PDF from URL
private void retrievePDFFromURL(String urlString){
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Handler handler = new Handler(Looper.getMainLooper());
    executorService.execute(() -> {
        // we are using inputstream
        // for getting out PDF.
        try {
            URL url = new URL(urlString);
            // below is the step where we are
            // creating our connection.
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            if (urlConnection.getResponseCode() == 200) {
                // response is success.
                // we are getting input stream from url
                // and storing it in our variable.
                inputStream = new BufferedInputStream(urlConnection.getInputStream());
            }
        } catch (IOException e) {
            // this is the method
            // to handle errors.
            e.printStackTrace();
        }
        handler.post(() -> {
            pdfView.fromStream(inputStream).load();
        });
    });
}