我知道如何使用AssetManager
从res/raw
目录中读取一个File
,并得到一个InputStream
对象。但是基于我的特殊需求,我需要一个FileInputStream
对象。我需要用getChannel()
方法从FileInputStream
对象中获取FileChannel
对象。
这是我目前的代码,它可以从File
中读取数据(在我的情况下是一组基本类型的列表):
public static int[] loadByMappedBuffer(Context context, String filename) throws IOException{
FileInputStream fis = context.openFileInput(filename);
FileChannel ch = fis.getChannel();
MappedByteBuffer mbuff = ch.map(MapMode.READ_ONLY, 0, ch.size());
IntBuffer ibuff = mbuff.asIntBuffer();
int[] array = new int[ibuff.limit()];
ibuff.get(array);
fis.close();
ch.close();
return array;
}
我曾尝试从Uri
创建File
,但结果只是一个FileNotFoundException
:
Uri uri = Uri.parse("android.resource://com.example.empty/raw/file");
File file = new File(uri.getPath());
FileInputStream fis = new FileInputStream(file);
有没有一种方法可以获取到在 res/raw
目录下的 File
的 FileInputStream
?
InputStream
。 - Mike Herasimovopen
方法返回InputStream
。 - Mike Herasimov