user2009775
user2009775

Reputation: 131

InputStream from Assets folder on Android returning empty

I'm not getting any exceptions, but when I run...

InputStream deckFile = context.getAssets().open("cards.txt");

Then, deckFile.read() returns -1. The file is in the correct folder, and it is NOT empty.

This should be the easiest thing in the world...

EDIT: The AssetManager is indeed listing "cards.txt" as being there, so that shouldn't be the problem.

Upvotes: 10

Views: 29059

Answers (2)

user2009775
user2009775

Reputation: 131

The problem was that my file was too big, and was being compressed because of it's ".txt" extension. By renaming the file to a format that is normally compressed, ".mp3", there was no issue

Upvotes: 3

Hardik Nadiyapara
Hardik Nadiyapara

Reputation: 2436

try below line of code

InputStream is = getAssets().open("test.txt");
int size = is.available();
byte[] buffer = new byte[size]; //declare the size of the byte array with size of the file
is.read(buffer); //read file
is.close(); //close file

// Store text file data in the string variable
    String str_data = new String(buffer);

the available method returns the total size of the asset...

Upvotes: 10

Related Questions