在android中加载文件
Android 似乎让加载某些类型的资源变得非常容易。一旦我稍微离开了人迹罕至的地方,它似乎就没那么有帮助了。当且仅当它们属于某些类型时,我才可以从资产文件夹中加载图像、声音和其他对象。如果我尝试加载我自己格式的二进制文件,我会得到一个不太有用的“文件未找到”异常,即使列出目录表明它显然在那里。
我尝试使用以下方法从资产目录中读取自定义格式的二进制文件:
File jfile = new File("file://android_asset/"+filename); //tried to get the URI of the assets folder
JarFile file = new JarFile("assets/"+filename); //tried assuming the assets folder is root
fd = am.openNonAssetFd( filename); //tried getting my file as an non asset in the assets folder (n.b. it is definitely there)
fs = am.open(filename, AssetManager.ACCESS_BUFFER); //tried loading it as an asset
我认为有一些关于 android 文件 I/O 的基本知识我还不明白。资产管理的文档似乎不完整,并且一定有某种原因故意使其不直观(与安全有关?)。那么,在 Android 应用程序中加载我自己格式的二进制文件的万无一失、规范的方法是什么?
更新:
我尝试了 file:///android_asset/ 但仍然没有乐趣。
String fullfilename = "file:///android_asset/"+filename;
File jfile = new File(fullfilename);
if (jfile.exists())
{
return new FileInputStream(jfile);
}
else
{
return null; //the file does exist but it always says it doesn't.
}
该文件或项目清单中是否有我需要的任何权限?
谢谢
Android seems to make life pretty easy for loading resources of certain types. Once I leave the beaten path a little bit, it seems less helpful. I can load in images, sounds and other objects from the assets folder if and only if they are of certain types. If I try to load a binary file of my own format, I get a less than helpful 'file not found' exception, even though listing the directory shows that it is clearly there.
I've tried using the following methods to read a binary file of a custom format from the assets directory:
File jfile = new File("file://android_asset/"+filename); //tried to get the URI of the assets folder
JarFile file = new JarFile("assets/"+filename); //tried assuming the assets folder is root
fd = am.openNonAssetFd( filename); //tried getting my file as an non asset in the assets folder (n.b. it is definitely there)
fs = am.open(filename, AssetManager.ACCESS_BUFFER); //tried loading it as an asset
I'm thinking that there's something fundamental about android file I/O that I don't understand yet. The documentation for asset management seems incomplete and there must be some reason for deliberately making this unintuitive (something to do with security?). So, what's the fool proof, canonical way of loading a binary file of my own format within an android app?
UPDATE:
I tried file:///android_asset/ but still no joy.
String fullfilename = "file:///android_asset/"+filename;
File jfile = new File(fullfilename);
if (jfile.exists())
{
return new FileInputStream(jfile);
}
else
{
return null; //the file does exist but it always says it doesn't.
}
Are there any permissions for the file or in the project manifest that I need?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为从 Assets 文件夹加载文件的最佳方法是使用 AssetManager.open(String filename) - 这会返回一个
InputStream
,然后您可以将其包装在BufferedInputStream
中并以其他方式调用read()
获取字节。无论文件类型如何,这都有效。您在使用这种方法时遇到了哪些具体问题?I think the best way to load a file from the Assets folder would be to use AssetManager.open(String filename) - this gives you back an
InputStream
which you can then wrap in aBufferedInputStream
and otherwise callread()
to get the bytes. This would work regardless of the file type. What kind of problems have you had with this approach specifically?我认为您遗漏了斜杠,如“
有三个正斜杠,而不是两个”。 :)
I think you have left out the slash as in
There's three forward slashes, not two. :)
对我来说,解决方案是卸载应用程序,在 Eclipse 中清理项目并再次运行它。问题是 Android 找不到我放入 asset 文件夹中的新文件。
我最终读了这个问题,所以我希望这对其他人有帮助。
For me the solution was to uninistall the application, clean the project in Eclipse and run it again. The problem was Android couldn't find the new files I put in the asset folder.
I ended up reading this question so I hope this can be helpful to someone else.