2013-07-21 101 views
0

我正在使用PDF查看器库在Android应用程序中查看PDF。我浏览了很多教程,其中之一是Dipak's tutorial。我想访问存储在我的资产文件夹中的PDF文件,而不是访问外部存储。我的问题是,我无法获得正确的“路径”。它总是返回找不到文件。PDF查看器库的路径

我试过以下,仍然得到同样的结果:

  • this.getAssets()
  • 文件:///android_assets/file_name.pdf
  • 文件:/// android_asset /file_name.pdf

回答

1

您无法从资产获取路径,必须在sd或内部内存中获取路径才能获取路径。

对于SD卡

先权限

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

然后将它卡写

File f = new File(Environment.getExternalStorageDirectory() + "file.pdf"); 
    if (!f.exists()) try { 

    InputStream is = getAssets().open("file.pdf"); 
    int size = is.available(); 
    byte[] buffer = new byte[size]; 
    is.read(buffer); 
    is.close(); 


    FileOutputStream fos = new FileOutputStream(f); 
    fos.write(buffer); 
    fos.close(); 
    } catch (Exception e) { throw new RuntimeException(e); } 

    Staring path = f.getPath(); 
+0

'文件f =新的文件( “目录中要放置” + “/file.pdf”);'会返回找不到的文件。由于该文件尚未写入目录 – Yukai

+0

请访问此链接以了解如何在SD卡中编写文件http://stackoverflow.com/questions/8181242/write-to-a-file-in-sd-card -in-android –

+0

试过这个'File f = Environment.getRootDirectory();'还没找到文件> _ _ – Yukai