2016-10-31 62 views
0

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

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

这里是我的清单文件权限:

<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

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

谢谢

+0

你能否发表一些更多的细节?例如项目结构。我认为文件路径是错误的...也许尝试添加“.pdf”到路径... –

+2

也看起来像你不应该把这个'src/main/assets/test'路径。尝试:pdfView.fromAsset(“test.pdf”)。在PDFView.fromAsset'context.getAssets()。open()'中使用http://stackoverflow.com/a/5771369/1533933,所以你不应该从源代码文件夹 – krossovochkin

+0

src/main/assets /测试?如果编译机器上的文件不在'..... assets/src/main/assets /'中,并且没有没有外部地址就不叫'test'。那么'FileNotFoundException是显而易见的... – Selvin

回答

1

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

请注意,我假设您在src/main/assets/中手动从文件中删除.pdf扩展名。如果该文件仍具有.pdf扩展名,则可能需要在代码中使用它。资源会删除其扩展名;资产没有。