android - 每三个应用程序从嵌入式文件中读取 android 上的 pdf

标签 android pdf

例如,我很难在 adobe 上阅读我的 pdf。我有 Document Viewer,它似乎在我购买之前已经安装在我的手机上(我认为它是默认的 android 应用程序)。有了它,我的代码工作正常。 但我见过崩溃,可能是那些没有文档查看器的人。

所以我用 adobe reader 测试了我的代码,它崩溃了!

谁能解释一下我的代码有什么问题(和/或)给我 Gplay 链接让我的用户下载它。

public Intent readPdf(String name, int res) {
    final String extStorageDirectory = Environment
            .getExternalStorageDirectory().toString();
    final String festivalDirectory_path = extStorageDirectory
            + Constants.PDF_STORAGE_PATH;
    File pdfOutputFile = new File(festivalDirectory_path, "/");
    if (pdfOutputFile.exists() == false) {
        pdfOutputFile.mkdirs();
    }
    File pdfFile = new File(pdfOutputFile, name);
    // AssetManager assetManager = getAssets();
    InputStream in = null;
    try {
        // InputStream in = assetManager
        // .open("cartecannesplus01pdf.pdf");
        in = getResources().openRawResource(res);

        OutputStream out = new FileOutputStream(pdfFile);
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ((bufferLength = in.read(buffer)) > 0) {
            out.write(buffer, 0, bufferLength);
        }
        out.flush();
        out.close();
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }
    Uri path = Uri.fromFile(pdfFile);


    Intent intent = null;
    if (Utils.appInstalledOrNot(me, "com.adobe.reader")) {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    } else {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=com.adobe.reader"));
    }

    return intent;

}

谢谢!

雷诺

最佳答案

我发现了问题: 参数中的名称必须以 .pdf 结尾

如果不是,则 adobe reader 无法阅读 pdf,但它可以与我尝试过的所有其他 pdf 阅读器一起使用!

谢谢大家!

关于android - 每三个应用程序从嵌入式文件中读取 android 上的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10977531/

相关文章:

java - Android将大输入流复制到文件非常慢

android - 无法将应用发布到 PlayStore - 禁止降级以前使用 M 权限的设备

c# - 打印Pdf文档C#字体生成的Barcode

java - 如何通过soapUI发送pdf文件的数组字节

Android 库、Kotlin 和 Dagger2

android - 在 Android 中更改 ActionMode 背景颜色时遇到问题

android - unity android wifi socket通信通过Android DLL

python - 如何以编程方式向 pdf 添加背景?

pdf - PDF 文档的大小,如何使用 Spire.pdf 从毫米转换为像素?

java - PDFBox改变一条线的颜色