4

我已经实现了一个自定义内容提供程序,以 ParcelFileDescriptor 的形式提供 pdf 文档。文件存储在标记为 PRIVATE 的本地存储中。然后基于 URI 将文档移交给选定的 pdf 应用程序。

这适用于除 adobe reader 之外的所有 PDF 查看器应用程序。有人可以确认 adobe reader 不适用于内容提供商吗?下面的代码:

下载文件后:

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);
    }
}

除 adobe reader 外,所有其他 pdf 查看器应用程序都调用此方法:

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 总是说:“无效的文件路径”

提前致谢!!!凯。

4

2 回答 2

1

据我所知,Adobe Reader 对从 ContentProviders 读取文件有不稳定的支持。在我的例子中,调用 openFile 方法并返回一个有效的 ParcelFileDescriptor,但 Adob​​e Reader 报告“无法打开文档”。我的内容提供程序可以很好地与“Drive PDF Viewer”、“PDF Reader”和“qPDF Reader”配合使用,它们是 Play 商店中的一些顶级 PDF 查看器。

Adobe Reader 能够使用 Gmail 内容提供程序在 Gmail 中打开 PDF 文件附件,但我无法确定其工作方式或原因。

于 2014-12-03T19:48:15.947 回答
0

我还遇到了 Adob​​e Acrobat Reader 无法与我的自定义提供程序一起使用的问题,最终让它正常工作。我的问题是应用程序本地文件空间中的文件是加密的,并且它们的名称是散列的。所以Uri是这样的:

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

它适用于我测试过的任何 PDF 查看器应用程序,除了 Adob​​e Reader。今天,我最后一次尝试向Content Uri添加.pdf扩展名(这绝对不是必需的),当 Adob​​e 调用一个openFile()函数时,我将扩展名去掉。瞧,它有效!

更新

请确保_display_name您的查询ContentProvider结果列也包含扩展名!query(Uri, String[], String, String[], String).pdf

笔记

使用Adob​​e Acrobat Reader 16.3版测试

于 2016-09-23T08:36:31.787 回答