简体   繁体   中英

Reading a file from a Res folder

I have put a file "template.html" inside RAW folder and I want to read it into a InputStream. But it is returning me null. Can't understand what is wrong in the below code

e.g. fileName passed as parameter is "res/raw/testtemplate.html"

public String getFile(String fileName) {
    InputStream input = this.getClass().getClassLoader().getResourceAsStream(fileName);
    return getStringFromInputStream(input);
}

Also, there might be a better solution by putting these files in a particular subfolder and putting it inside Asset folder but then I believe I would need to pass context in AssetManager. I don't understand that solution, sorry I am new to android development. Can someone shed some light regarding how this approach can be achieved.

EDIT

I have started implementing this solution with Assets. Below method is supposed to return a string containing the entire text of the file stored as template.html.

getFile("template.html") // I am sending extension this time

Problem getting error getAssets() is undefined.

public String getFile(String fileName) {

    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    String line;
    try {
          reader = new BufferedReader(new InputStreamReader(getAssets().open(fileName)));
          while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }
    catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();

}

use this

new BufferedInputStream(getResources().openRawResource(filepath));

this will return a buffered input stream

The file name should be without extension :

  InputStream ins = getResources().openRawResource(
                getResources().getIdentifier("raw/FILENAME_WITHOUT_EXTENSION",
                "raw", getPackageName()));

For this purposes uses assets folder:

assets/

This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

So, you could easy get access at assets with context: context.getAssets()

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(context.getAssets().open("filename.txt")));

    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM