在Android平台上访问应用程序的项目文件
因此,在我的 Eclipse android 项目中,我有一个想要打开的 pdf 文件,我在 android 开发人员页面上查找了标准地址,并找到了这个指针:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
其中 jazz.pdf 是位于我的 Eclipse 项目中的 res->raw
中, com.alex.testing 是我的包名称。
不过,当我尝试 if(file.exists())
时,该函数返回 false (在模拟器上,它会转到我设置的显示错误消息的 else )...
抱歉新手问题,但我真的很困惑:(。
So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw
in my eclipse project,
and com.alex.testing
is my package name.
Still, when I try if(file.exists())
, the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,要从当前应用程序访问资源,您可以使用类似的内容,
或者
但是我怀疑您是否尝试在应用程序中使用此 pdf 文件那么没问题,但是如果您想使用任何第三方应用程序查看此文件,那么我认为你做不到。
因为外部应用程序无法访问应用程序的包资源文件。
因此更好的方法是将这个文件放在
/asset
目录中,然后将其复制到任何公共访问区域然后查看该文件从那条路。Ok, to access resources from current application you can use something like,
OR
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in
/asset
directory then copy it to any public access area then view that file from that path.//如果您存储在SD卡中,您的位置将是
//您通过以下代码从原始文件夹中读取资源
//if your are stored in SDcard your location would be
//you read resources from raw folder thru the below code
将文件放入资产文件夹中并从那里选择文件
现在使用 Context.getAssets().open("jazz.pdf") 并将生成的
InputStream
传递到 PDF 解析器图书馆put the file in assets folder and pick the file from there
Now use
Context.getAssets().open("jazz.pdf")
and pass the resultingInputStream
into PDf parser library