我已经实现了一个自定义内容提供程序,以 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 总是说:“无效的文件路径”
提前致谢!!!凯。