java 在android中加载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6972282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading files within android
提问by Luther
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.
Android 似乎让加载某些类型的资源变得非常容易。一旦我稍微离开了人迹罕至的地方,它似乎就没那么有用了。当且仅当它们属于某些类型时,我才能从资产文件夹加载图像、声音和其他对象。如果我尝试加载我自己的格式的二进制文件,我会得到一个不太有用的“找不到文件”异常,即使列出目录表明它显然存在。
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?
我在想关于 android 文件 I/O 的一些基本知识我还不明白。资产管理的文档似乎不完整,必须有某种原因故意使这变得不直观(与安全有关?)。那么,在 android 应用程序中加载我自己格式的二进制文件的最简单的、规范的方法是什么?
UPDATE:
更新:
I tried file:///android_asset/ but still no joy.
我试过 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.
}
Are there any permissions for the file or in the project manifest that I need?
我需要的文件或项目清单中是否有任何权限?
Thanks
谢谢
回答by antonyt
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 a BufferedInputStream
and otherwise call read()
to get the bytes. This would work regardless of the file type. What kind of problems have you had with this approach specifically?
我认为从 Assets 文件夹加载文件的最佳方法是使用AssetManager.open(String filename)- 这会给你一个InputStream
,然后你可以将它包装在 a 中BufferedInputStream
,否则调用read()
以获取字节。无论文件类型如何,这都可以工作。您在使用这种方法时遇到了哪些具体问题?
回答by t0mm13b
I think you have left out the slash as in
我想你已经省略了斜线
File jfile = new File("file:///android_asset/"+filename);
There's three forward slashes, not two. :)
有三个正斜杠,而不是两个。:)
回答by Ena
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.
对我来说,解决方案是卸载应用程序,在 Eclipse 中清理项目并再次运行它。问题是 Android 找不到我放在资产文件夹中的新文件。我最终阅读了这个问题,所以我希望这对其他人有帮助。