DImuthuUpe / AndroidPdfViewer

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

FromUri doesn't work #1062

Open Siahkali opened 2 years ago

Siahkali commented 2 years ago

Hi FromUri doesn't work "Without any errors and reactions"

        Uri uri = Uri.parse(BaseURL + "67038.pdf");
        pdfView.fromUri(uri).defaultPage(1)
                .onPageChange(new OnPageChangeListener() {
                    @Override
                    public void onPageChanged(int page, int pageCount) {
                        Log.e("onPageChanged", "onPageChanged");
                    }
                })
                .enableAnnotationRendering(true)
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {
                        Log.e("loadComplete", "loadComplete");
                    }
                })
                .scrollHandle(new DefaultScrollHandle(getContext()))
                .spacing(10)
                .onPageError(new OnPageErrorListener() {
                    @Override
                    public void onPageError(int page, Throwable t) {
                        Log.e(t.getMessage(), t.getMessage());
                    }
                })
                .load();
gopalsays108 commented 2 years ago

Hi, You can not load pdf directly from the URL. First, you need to download it to the local storage of your device and then load it using the file like this

String url = BaseURL + "67038.pdf";
AsyncTask.execute( new Runnable() {
                @Override
                public void run() {
                    try {
                        final InputStream input = new URL( url ).openStream();

                        runOnUiThread( new Runnable() {
                            @Override
                            public void run() {

                                pdfView.fromStream( input )
                                        .defaultPage( finalDefaultValue )
                                        .enableSwipe( true )
                                        .swipeHorizontal( true )
                                        .pageSnap( true )
                                        .autoSpacing( true )
                                        .nightMode( isChecked )
                                        .pageFling( true )
                                        .enableAnnotationRendering( true )
                                        .scrollHandle( new DefaultScrollHandle( mContext ) )
                                        .load();
                                progressBar.setVisibility( View.INVISIBLE );
                            }
                        } );
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } );

Let me know if it helps you

Siahkali commented 2 years ago

Ok thanks