Android: getAssets().openFd() and FileNotFoundException

19,846

Solution 1

try this :

AssetFileDescriptor descriptor = getAssets().openFd("openAccess.txt");
BufferedReader f = new BufferedReader(new FileReader(descriptor.getFileDescriptor()));
String line = f.readLine();
while (line != null) {
    // do stuff
    Log.d("TAG",line);
}

Solution 2

How about this:

InputStream in = context.getAssets().open("openAccess.txt");
reader = new InputStreamReader(in);

Solution 3

What did it for me was to create a "row"-folder in /res and to copy the files in there. Then you can use:

InputStreamReader iReader = new InputStreamReader(getResources().openRawResource(R.raw.text)));
Share:
19,846
Milos Cuculovic
Author by

Milos Cuculovic

Masters degree in Embedded and comunicant systems.

Updated on June 05, 2022

Comments

  • Milos Cuculovic
    Milos Cuculovic about 2 years

    I am trying to read a txt file from assets folder like that:

    descriptor = context.getAssets().openFd("openAccess.txt");
    reader = new FileReader(descriptor.getFileDescriptor());
    

    but I am getting this exception:

    java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

    I don't know what is the problem?

  • Milos Cuculovic
    Milos Cuculovic about 12 years
    still the same problem: fileNotFoundException
  • SBJ
    SBJ about 12 years
    Please check this comment stackoverflow.com/questions/6186866/…
  • Milos Cuculovic
    Milos Cuculovic about 12 years
    Still the same problem @David Wasser
  • David Wasser
    David Wasser about 12 years
    Are you sure that the text file is actually present in your assets?
  • Milos Cuculovic
    Milos Cuculovic about 12 years
    Oh, it works now, sorry, i did a litle mistake. The other problem now is that I am getting the file adress like java.io.InputStreamReader@405248a0