InputStream from Assets folder on Android returning empty

26,350

Solution 1

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

Solution 2

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...

Solution 3

Place your text file in the /assets directory under the Android project. Use AssetManager class to access it.

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

Or you can also put the file in the /res/raw directory, where the file will be indexed and is accessible by an id in the R file:

InputStream is = getResources().openRawResource(R.raw.test);

EDITED:

Try out the below method to read your file:

 public String convertStreamToString(InputStream p_is) throws IOException {
    /*
     * To convert the InputStream to String we use the
     * BufferedReader.readLine() method. We iterate until the BufferedReader
     * return null which means there's no more data to read. Each line will
     * appended to a StringBuilder and returned as String.
     */
    if (p_is != null) {
        StringBuilder m_sb = new StringBuilder();
        String m_line;
        try {
            BufferedReader m_reader = new BufferedReader(
                    new InputStreamReader(p_is));
            while ((m_line = m_reader.readLine()) != null) {
                m_sb.append(m_line).append("\n");
            }
        } finally {
            p_is.close();
        }
        Log.e("TAG", m_sb.toString());
        return m_sb.toString();
    } else {
        return "";
    }
}

I am sure it will help you.

Share:
26,350
user2009775
Author by

user2009775

Updated on July 09, 2022

Comments

  • user2009775
    user2009775 almost 2 years

    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.

  • user2009775
    user2009775 over 11 years
    It looks like it's supposed to take an int, so I tried InputStream deckFile = context.getResources().openRawResource(R.raw.cards); and that didn't work either.
  • GrIsHu
    GrIsHu over 11 years
    Where have you kept your file in assests folder of res/raw folder?
  • GrIsHu
    GrIsHu over 11 years
    Please check out my answer i have edited it and try to access it that way.
  • user2009775
    user2009775 over 11 years
    I have a file in /assets and /res/raw called "cards.txt". It's not throwing an exception when I call it, and the "list" function of the AssetManager is listing "cards.txt" as there, but for some reason it's reading the file as an empty file rather than a long text file...
  • Hardik Nadiyapara
    Hardik Nadiyapara over 11 years
    this the size of the file in bytes
  • user2009775
    user2009775 over 11 years
    Thanks, but it's not solving my problem. The problem is that InputStream seems to think there's nothing there.
  • user2009775
    user2009775 over 11 years
    Well, I'm now getting an IOException with your method, which is... something. I'll figure this out. Thanks.
  • user2009775
    user2009775 over 11 years
    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.
  • hotHead
    hotHead about 8 years
    haha, I've had the same troble, but I've changet file extension to jpg
  • shiami
    shiami almost 7 years
    The buffer size may cause OOM on some devices.