如何获得的FileInputStream的资产文件夹到文件 [英] How to get FileInputStream to File in assets folder

查看:226
本文介绍了如何获得的FileInputStream的资产文件夹到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 AssetManager 文件 RES /原料的InputStream ,但我的特殊用途的情况下,我需要一个的FileInputStream 目录。我需要一个的FileInputStream 特别是因为我需要从它的 FileChannel 对象通过调用<$ C $的原因C> getChannel()。

I know how to use the AssetManager to read a File from the res/raw directory with an InputStream, but for my special use case I need a FileInputStream. The reason I need a FileInputStream specifically is because I need to get the FileChannel object from it by calling getChannel().

这是在code我到目前为止,它读取数据(在我的情况下,基元的列表)从文件

This is the code I have so far, it reads the data (in my case a list of primitives) from a 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;
} 

我曾试图创建文件乌里,但只是导致 FileNotFoundException异常

Uri uri = Uri.parse("android.resource://com.example.empty/raw/file");
File file = new File(uri.getPath());
FileInputStream fis = new FileInputStream(file);

有没有一种方法可以让我得到一个的FileInputStream 文件水库/生目录?

推荐答案

您可以得到一个的FileInputStream 来在你的资产,资源是这样的:

You can get a FileInputStream to a resource in your assets like this:

AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
FileInputStream stream = fileDescriptor.createInputStream();

文件名您提供的 openFd()应该是相对路径的资产,同样的文件名,你会提供给的open()

The fileName you supply to openFd() should be the relative path to the asset, the same fileName you would supply to open().

另外,您还可以创建的FileInputStream 是这样的:

Alternatively you can also create the FileInputStream like this:

AssetFileDescriptor assetFileDescriptor = assetManager.openFd(fileName);  
FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();  
FileInputStream stream = new FileInputStream(fileDescriptor);

这篇关于如何获得的FileInputStream的资产文件夹到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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