Android PdfViewer-FileNotFoundException
Asked Answered
C

4

7

I installed this library to view pdf from a url inside my application but when I click on the file to open it gives me the following error:

I tried looking into ContentResolver but no luck

This is the logcat:

07-26 15:35:45.638 8725-8725/com.focuson.iapp.firstapp E/PDFView: load pdf error
java.io.FileNotFoundException: No content provider: http://pub.mylaravel.eu/assets/user_files/3/file_no_1.pdf
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1093)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:944)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:797)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:751)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.getSeekableFileDescriptor(DecodingAsyncTask.java:82)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:61)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:30)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

My activity is:

public class pdfActivity extends AppCompatActivity {
PDFView pdfView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdf);
    pdfView= (PDFView) findViewById(R.id.pdfView);
    String url=getIntent().getStringExtra("url");
    Log.i("URI-URL",String.valueOf(Uri.parse(url)) );
    pdfView.fromUri(Uri.parse(url))
            .enableSwipe(true)
            .enableDoubletap(true)
            .swipeVertical(false)
            .defaultPage(1)
            .showMinimap(false)
            .onLoad(new OnLoadCompleteListener() {
                @Override
                public void loadComplete(int nbPages) {

                }
            })
            .onPageChange(new OnPageChangeListener() {
                @Override
                public void onPageChanged(int page, int pageCount) {

                }
            })
            .enableAnnotationRendering(false)
            .password(null)
            .load();}}

My xml is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.focuson.iapp.firstapp.page_types.Login.pdfActivity">

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>
Corneliuscornell answered 26/7, 2016 at 12:52 Comment(3)
Can you access that URL on the phone's browser?Gounod
Yes and it begins downloading the pdf on my phoneCorneliuscornell
I'm guessing that the library expects a ContentProvider URI, not a network addressGounod
P
7

Seems that this library fails to download files from the internet. Download the file to a temp directory and open it:

if (ActivityCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(WebViewActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        } else {
            downloadFile();
        }

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
            downloadFile();
            break;
    }
}

private void downloadFile() {
    progressBar.setVisibility(View.VISIBLE);
    DownloadFileTask task = new DownloadFileTask(
            WebViewActivity.this,
            mURL,
            "/download/pdf_file.pdf");
    task.startTask();
}

@Override
public void onFileDownloaded() {
    progressBar.setVisibility(View.GONE);
    File file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath()
            + "/download/pdf_file.pdf");
    if (file.exists()) {
        pdfView.fromFile(file)
                //.pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
                .enableSwipe(true)
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(true)
                .password(null)
                .scrollHandle(null)
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {
                        pdfView.setMinZoom(1f);
                        pdfView.setMidZoom(5f);
                        pdfView.setMaxZoom(10f);
                        pdfView.zoomTo(2f);
                        pdfView.scrollTo(100,0);
                        pdfView.moveTo(0f,0f);
                    }
                })
                .load();

    }
}

public class DownloadFileTask {
public static final String TAG = "DownloadFileTask";

private BaseActivity context;
private GetTask contentTask;
private String url;
private String fileName;

public DownloadFileTask(BaseActivity context, String url, String fileName) {
    this.context = context;
    this.url = url;
    this.fileName = fileName;
}

public void startTask() {
    doRequest();
}

private void doRequest() {
    contentTask = new GetTask();
    contentTask.execute();
}

private class GetTask extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... arg0) {
        int count;
        try {
            Log.d(TAG, "url = " + url);
            URL _url = new URL(url);
            URLConnection conection = _url.openConnection();
            conection.connect();
            InputStream input = new BufferedInputStream(_url.openStream(),
                    8192);
            OutputStream output = new FileOutputStream(
                    Environment.getExternalStorageDirectory() + fileName);
            byte data[] = new byte[1024];
            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return null;
    }

    protected void onPostExecute(String data) {
        context.onFileDownloaded();
    }
}

}

Pines answered 28/3, 2017 at 11:15 Comment(1)
thank you so much sir you save my day thanks again <3Bivins
D
2

Uri is not like URL :here

You must download first the *.pdf file and save to SD and finaly you can open it with Pdfview.

Destrier answered 26/7, 2016 at 13:32 Comment(1)
"Library doesn't support remote content, so it works as expected. Glad that update fixed loading from internal memory." from github.com/barteksc/AndroidPdfViewer/issues/2Staphylorrhaphy
G
1

No content provider: http://...

It's not clear where you read that the library can download and display a PDF from a network request. A URI can mean many things, and in this case, it is trying to use a ContentProvider to load a raw PDF file from disk.

You can look at the Github repo for that library for sample usage of the URI.

Gounod answered 26/7, 2016 at 13:28 Comment(0)
E
0

You can use an InputStream if you don't want to download the file localy:

        new Thread(() -> {
            try {
                InputStream input = new URL(url).openStream();
                runOnUiThread(() -> pdfView.fromStream(input)
                        .defaultPage(0)
                        .enableSwipe(true)
                        .swipeHorizontal(false)
                        .enableAnnotationRendering(true)
                        .scrollHandle(new DefaultScrollHandle(YourActivity.this))
                        .load());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
Erect answered 23/11, 2023 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.