Mayank Kumar Chaudhari
Mayank Kumar Chaudhari

Reputation: 18618

How to Get parcelFileDescriptor from URI?

In my app I am receiving intent to open the pdf file using "application/pdf" intent-filter in menifest.

After a lot of research I am trying following code to open the file.

     try {
            File f = new File("file://"+uri.getPath());
            Toast.makeText(k.this, f.getAbsolutePath(), Toast.LENGTH_LONG).show();
            Toast.makeText(k.this, f.exists()?"Y":"N", Toast.LENGTH_LONG).show();
            parcelFileDescriptor = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (IOException e) {
            e.printStackTrace();
        }

When I click on a file from file manager and open it with my app, It says the file does not exist. That means I am not able to create file from URI.

So my question is how to get parcelFileDescriptor from URI.

Upvotes: 3

Views: 2828

Answers (1)

Mayank Kumar Chaudhari
Mayank Kumar Chaudhari

Reputation: 18618

Thanks to the comment of Mike M.

getContentResolver().openFileDescriptor()

opens the fileDescriptor correctly.

Upvotes: 7

Related Questions