'Android: Cannot read PDF File user selected using BOTH getString() and getPath() from the URI

I am using the PdfBox-Android (https://github.com/TomRoush/PdfBox-Android) library to read in PDF files. When the user clicks on a button (w/ id file1), a file picker pops up where he or she can choose a pdf file to read in. However, when I get the URI from the user's selected file and use EITHER getString() or getPath() to get the file name, I receive the following error (using toString()):

Exception: java.io.FileNotFoundException: content:/com.android.providers.downloads.documents/document/7306: open failed: ENOENT (No such file or directory)

And the following error using getPath():

Exception java.io.FileNotFoundException: /document/7097: open failed: ENOENT (No such file or directory)

Below is my code:

import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.text.PDFTextStripper
import com.tom_roush.pdfbox.util.PDFBoxResourceLoader

// File picker implementation
private fun chooseFile(view:View) {
    println("chooseFile activated!");
    var selectFile = Intent(Intent.ACTION_GET_CONTENT)
    selectFile.type = "*/*"
    selectFile = Intent.createChooser(selectFile, "Choose a file")
    startActivityForResult(selectFile, READ_IN_FILE)
}


/* After startActivityForResult is executed, when the selectFile Intent is completed, onActivityResult is executed with
   the result code READ_IN_FILE.*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == READ_IN_FILE) { // Step 1: When a result has been received, check if it is the result for READ_IN_FILE
        if (resultCode == Activity.RESULT_OK) { // Step 2: Check if the operation to retrieve thea ctivity's result is successful
            // Attempt to retrieve the file
            try {
                // Retrieve the true file path of the file
                var uri: Uri? = data?.getData();
                    // Initialize and load the PDF document reader for the file the user selected
                    var document = PDDocument.load(File(uri?.path));

                    // Read in the text of the PDF document
                    var documentText = PDFTextStripper().getText(document);

                    println("documentText = " + documentText);

                    document.close();
            } catch (e: Exception) { // If the app failed to attempt to retrieve the error file, throw an error alert
                println("EXCEPTION: " + e.toString());
            }
        }
    }
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Initialize the PDFBox library's resource loader
    PDFBoxResourceLoader.init(applicationContext);

    setContentView(R.layout.activity_main)

    var file1:Button = findViewById(R.id.file1);
    file1.setOnClickListener(::chooseFile)
}


Solution 1:[1]

Before calls to PDFBox are made it is highly recommended to initialize the library's resource loader. Add the following line before calling PDFBox methods:

PDFBoxResourceLoader.init(getApplicationContext());

So edit your code

.....
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)

   PDFBoxResourceLoader.init(getApplicationContext());//call this
   var file1:Button = findViewById(R.id.file1);
   file1.setOnClickListener(::chooseFile)
}

Solution 2:[2]

You can get the inputstream for the uri like this to load the pdf file

PDDocument pdDocument = PDDocument.load(context.getContentResolver().openInputStream(uri));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Md. Enamul Haque
Solution 2 kundan singh