【问题标题】:Android Pdf Viewer FileNotFoundExceptionAndroid Pdf 查看器 FileNotFoundException
【发布时间】:2016-10-31 10:50:38
【问题描述】:

我正在尝试在我的应用程序中显示 pdf 文件。我正在使用 Pdf 查看器库(https://github.com/barteksc/AndroidPdfViewer)。我在我的资产文件夹中放置了一个 pdf 文件。但是当我尝试加载文件时,它显示了这个异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samsung.secautomation/com.samsung.secautomation.ui.activities.ShowPdfActivity}: com.github.barteksc.pdfviewer.exception.FileNotFoundException: src/main/assets/test does not exist

这是我的代码:

com.github.barteksc.pdfviewer.PDFView pdfView=(com.github.barteksc.pdfviewer.PDFView) findViewById(R.id.pdfViewer);
    pdfView.fromAsset("src/main/assets/test") //test is the pdf file name
            .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
            .enableSwipe(true)
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .defaultPage(0)
            .enableAnnotationRendering(false)
            .password(null)
            .scrollHandle(null)
            .load();

这是我在 Manifest 文件中的许可:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

我使用的是 Ubuntu 12.04

有什么办法可以解决这个问题。

谢谢

【问题讨论】:

  • 你能发布更多细节吗?例如项目结构。我认为文件路径错误...也许尝试将“.pdf”添加到路径...
  • 看起来你不应该把这个'src/main/assets/test'路径。尝试:pdfView.fromAsset("test.pdf")。在 PDFView.fromAsset 'context.getAssets().open()' 内部使用 stackoverflow.com/a/5771369/1533933 所以你不应该从你的源代码文件夹写路径
  • src/main/assets/test ?如果编译机上的文件不在.....assets/src/main/assets/ 中,并且在没有分机的情况下不被称为test。那么 `FileNotFoundException 很明显......
  • pdfView.fromAsset() 仅将字符串作为参数,这就是我放置文件位置的原因。应用结构为Project-->app-->src-->main-->assets-->test.pdf
  • 资产位于“预定义”位置,不参考您的构建路径。所有 Android 资产文件都是如此。

标签: android pdfviewer


【解决方案1】:

src/main/assets/test 替换为testsrc/main/assets/test 是该文件的相对路径在您的开发机器上,而不是在设备上打包的资产中。

请注意,我假设您在将 src/main/assets/ 放入文件时手动从文件中删除了 .pdf 扩展名。如果该文件仍然具有 .pdf 扩展名,则您的代码中可能需要它。资源放弃它们的扩展;资产没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2012-03-16
    • 2011-06-03
    • 2011-06-28
    相关资源
    最近更新 更多