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)));
Author by
Milos Cuculovic
Masters degree in Embedded and comunicant systems.
Updated on June 05, 2022Comments
-
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 about 12 yearsstill the same problem: fileNotFoundException
-
SBJ about 12 yearsPlease check this comment stackoverflow.com/questions/6186866/…
-
Milos Cuculovic about 12 yearsStill the same problem @David Wasser
-
David Wasser about 12 yearsAre you sure that the text file is actually present in your assets?
-
Milos Cuculovic about 12 yearsOh, 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