necip
necip

Reputation: 353

pdfviewer doesnt open File

myfile.exists() return TRUE, however I couldn't open the pdf:

File myfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) 
        + File.separator + fileString ); //fileString=sample.pdf
Log.i("here", fileString + "---"+ myfile.exists());

pdfView = findViewById(R.id.pdfViewer);
pdfView.fromFile(myfile)
        .defaultPage(0)
        .spacing(2)
        .load();

I'm using this library for Pdf view in android.

implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

If you wish, I can post all-working snippet code.

(using pdfView.fromUri() works fine but only once. My aim is to pick a .pdf from Intent.ACTION_OPEN_DOCUMENT once and save the uri of it for next usage.

Upvotes: 0

Views: 239

Answers (1)

necip
necip

Reputation: 353

Instead of pdfView.fromFile(), I used pdfView.fromUri(),

pdfView = findViewById(R.id.pdfViewer);
//pdfView.fromFile(myfile)
pdfView.fromUri(Uri.parse(getIntent_uri))
    .defaultPage(0)
    .spacing(2)
    .load();

then after picking file, in onActivityResult() I used takePersistableUriPermission() for make the uri permanent accessible.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_PDF_FILE && resultCode == Activity.RESULT_OK && data != null) {
        Uri uri = data.getData();
        
        getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

special thanks to @CommonsWare

Upvotes: 0

Related Questions