如何从R.ra​​w获取文件 [英] How to get File from R.raw

查看:120
本文介绍了如何从R.ra​​w获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到一个文件的字节数组,所以我需要知道该文件的长度。
我怎样才能从资产或原始目录中的文件?

I need to get an array of bytes of a File, so I need to know the length of the file. How can I get a file from the assets or raw directory ?

看来最正常的是获得的InputStream,但不能获取文件的长度。

It seems that the most normal is to get the InputStream, but then can't get the length of the file.

我如何从R.ra​​w文件?

How can I File from R.raw ?

推荐答案

1,将您TESTFILE.TXT文件到 RES /生/

1, place your testfile.txt file into res/raw/

2,获得该文件像下面

2, get this file like below

try {
    InputStream inputStream = getResources().openRawResource(R.raw.TestFile);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    int length;
    while ((length = inputStream.read()) != -1) {
        stream.write(length);
    }
    return stream.toString();
} catch (IOException e) {
    return "";
}

这篇关于如何从R.ra​​w获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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