首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Adobe Reader显示PDF文件

使用Adobe Reader显示PDF文件
EN

Stack Overflow用户
提问于 2012-08-03 14:03:38
回答 2查看 3.1K关注 0票数 0

我创建了一个类openPDF,它接受一个字节数组作为输入,并使用Adobe Reader显示该文件。代码:

代码语言:javascript
复制
private void openPDF(byte[] PDFByteArray) {


    try {
        // create temp file that will hold byte array
        File tempPDF = File.createTempFile("temp", ".pdf", getCacheDir());
        tempPDF.deleteOnExit();

        FileOutputStream fos = new FileOutputStream(tempPDF);
        fos.write(PDFByteArray);
        fos.close();

        Intent intent = new Intent();
           intent.setAction(Intent.ACTION_VIEW);
           Uri uri = Uri.fromFile(tempPDF);
           intent.setDataAndType(uri, "application/pdf");
           intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     

           startActivity(intent);


    } catch (IOException ex) {
        String s = ex.toString();
        ex.printStackTrace();
    }
}

当我传递意图时,来自adobe阅读器的错误是“无效的文件路径”。我阅读了所有其他与在android中下载和查看PDF相关的帖子,但帮助不大。有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-26 21:14:00

我写这段代码是为了用Adobe的应用程序打开Dowloads文件夹中的一个特定的.pdf文件

代码语言:javascript
复制
    File folder = new File(Environment.getExternalStorageDirectory(), "Download");
    File pdf = new File(folder, "Test.pdf");

    Uri uri = Uri.fromFile(pdf);

    PackageManager pm = getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage("com.adobe.reader");
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);

这对我很管用。所以我猜你的问题可能是临时文件。尝试将文件写入sdcard。为此,您需要在AndroidManifest.xml中添加android.permission.WRITE_EXTERNAL_STORAGE

票数 0
EN

Stack Overflow用户

发布于 2012-08-03 16:52:39

我认为问题在于其他应用程序无法访问您应用程序的私有数据区(如缓存目录)中的文件。

候选解决方案:

  1. 将文件模式更改为MODE_WORLD_READABLE,以便其他应用程序可以读取该文件

..。字符串fn = "temp.pdf";上下文c= v.getContext();FileOutputStream fos = null;try { fos = c.openFileOutput(fn,Context.MODE_WORLD_READABLE);fos.write(PDFByteArray);} catch (FileNotFoundException e) { //做某事} catch (IOException e) { // TODO自动生成的catch块e.printStackTrace();}最终{ if (fos!=null) { try { fos.close();} catch (IOException e) { // TODO自动生成的捕获块e.printStackTrace();} intent = new Intent();intent.setAction(Intent.ACTION_VIEW);字符串filename = c.getFilesDir() + File.separator + fn;File file = new file(文件名);Uri uri =Uri.fromFile(文件);intent.setDataAndType(uri,“应用程序/pdf”);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent);...

  • 或将pdf文件写入/sdcard分区。

您可以使用android.os.Environment接口获取路径,记得将权限添加到您的应用的AndroidManifest.xml文件中。

问候

陈子腾

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11790152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档