Kay
Kay

Reputation: 107

Can somebody confirm that adobe reader works with custom content provider

I have implemented a custom content provider serving pdf documents as ParcelFileDescriptor. Files are stored in the local storage marked as PRIVATE. Based on an URI the documents are then handed over to the selected pdf application.

This works for all PDF Viewer applications except adobe reader. Can someone please confirm that adobe reader does not work with content providers? Code below:

When document has been downloaded:

private void loadDocInReader(String doc) throws ActivityNotFoundException, Exception 
{
    Uri uri = Uri.parse(doc);   

    logger.debug("PDF Application ID is: " + pdfAppID);

    if (this.pdfAppID != null && this.pdfAppID.length() > 0) 
    {
        boolean pdfApplicationIsInstalled = checkPDFApplicationIsInstalled(this.pdfAppID);

        if(pdfApplicationIsInstalled) {
            Intent intent = new Intent();
            intent.setPackage(pdfAppID);
            intent.setData(uri);
            intent.setType("application/pdf");
            startActivity(intent);
        }
        else {
            logger.error("Please install Adobe Reader first!");
        }
    }
    else {
        Intent intent = new Intent();
        intent.setData(uri);
        intent.setType("application/pdf");
        startActivity(intent);
    }
}

All other pdf viewer apps call this method except adobe reader:

public class DocumentProvider extends ContentProvider 
{
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException 
{
    File file = null;

    try {

        file = new File(uri.getPath());
        logger.debug("Delivering ParcelFileDescriptor for path: " + file.getPath());
        return ParcelFileDescriptor.open(file,   ParcelFileDescriptor.MODE_READ_ONLY);

    } catch (FileNotFoundException e) {
        logger.error("Error loading Document: ",e);
    } finally {
        if(file.exists()) {
        file.delete();
        }
    }
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    return 0;
}
}

Adobe Reader always states: "Invalid File Path"

Thanks in advance!!! Kay.

Upvotes: 4

Views: 864

Answers (2)

xsveda
xsveda

Reputation: 17894

I has also issues with Adobe Acrobat Reader not working with my custom provider and finally got it working. My problem was that files in app-local file-space are encrypted and their names are hashed. So the Uri was something like:

content://my.app.provider/08deae8d9ea9bc0b84f94475d868351830e9f7e7

It works with any PDF viewer app I've tested except Adobe Reader. Today I did one last try to add a .pdf extension to the Content Uri (which is definitely not required) and when Adobe calls an openFile() function I strip the extension away. VOILA, it works!!!

Update

Please make sure that also _display_name column your ContentProvider returns as result of query(Uri, String[], String, String[], String) query also contains .pdf extension!!!

Note

Tested with Adobe Acrobat Reader version 16.3

Upvotes: 0

plinehan
plinehan

Reputation: 760

As far as I can determine Adobe Reader has flaky support for reading files from ContentProviders. In my case, the openFile method is called and returns a valid ParcelFileDescriptor, but Adobe Reader reports "The document could not be opened." My content provider works fine with "Drive PDF Viewer", "PDF Reader" and "qPDF Reader", which are some of the top PDF viewers in the Play Store.

Adobe Reader is able to open PDF file attachments in Gmail using the Gmail content provider, but I cannot determine how or why that works.

Upvotes: 1

Related Questions