0

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 ?

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

How can I File from R.raw ?

Fischer
  • 1,513
  • 4
  • 17
  • 38

2 Answers2

2

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

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 "";
}
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
0

Try this and see if it works:

file:///android_asset/yourfile
android.resource://package_name/raw/yourfile
Anthony
  • 12,177
  • 9
  • 69
  • 105
JohnCookie
  • 661
  • 3
  • 7