安卓中加载文件 [英] Loading files within android

查看:401
本文介绍了安卓中加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来Android让生活pretty容易为某些类型的加载资源。一旦我离开常走的道路一点点,似乎没有多大帮助。我可以加载在图像,声音和其他对象从资产文件夹中当且仅当他们是某些类型的。如果我尝试加载我自己格式的二进制文件,我得到比一个有用的少文件未找到例外,即使列出该目录显示,这是明显存在的。

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

我在想,有一些关于Android的文件I / O,我还不知道根本。资产管理的文档似乎不完整的,必须有某种原因故意使这一直观(事做保安?)。那么,什么是傻瓜证明,一个Android应用程序加载在我自己格式的二进制文件的规范的方法?

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_asset /但仍然没有喜悦

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?

感谢

推荐答案

我觉得加载从资产文件夹中的文件,最好的办法是使用<一个href=\"http://developer.android.com/reference/android/content/res/AssetManager.html#open%28java.lang.String%29\"相对=nofollow> AssetManager.open(字符串文件名) - 这给你回一个的InputStream 然后可以包装在的BufferedInputStream 否则叫阅读()来得到字节。无论文件类型的这会工作。你有过这种方法具体是什么样的问题?

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?

这篇关于安卓中加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆