0

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();

}
kafan1986
  • 5
  • 1
  • 8
  • possible duplicate of [How to read file from res/raw by name](http://stackoverflow.com/questions/15912825/how-to-read-file-from-res-raw-by-name) – Phantômaxx Oct 28 '14 at 08:12

3 Answers3

1

use this

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

this will return a buffered input stream

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
1

The file name should be without extension :

  InputStream ins = getResources().openRawResource(
                getResources().getIdentifier("raw/FILENAME_WITHOUT_EXTENSION",
                "raw", getPackageName()));
Zied R.
  • 4,964
  • 2
  • 36
  • 67
0

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
         }
    }
}
Tpec1k
  • 363
  • 2
  • 10
  • I tried implementing your solution. getAssets is not defined error is coming. Do I need to import something? – kafan1986 Oct 28 '14 at 09:02
  • You have to create assets folder: src/main/assets (for Android Studio), and try http://developer.android.com/reference/android/content/Context.html#getAssets() – Tpec1k Oct 28 '14 at 09:39
  • Please go through the Edit in the main question body getAssets method is not defined. Error is coming before compiling. – kafan1986 Oct 28 '14 at 10:09
  • getAssets() is member of context, you need a context instance of course. I've sent a link on the documentation about it above – Tpec1k Oct 28 '14 at 10:11
  • How to do that? Currently this class does not extend activity. Also, should I include import android.app.Activity; import android.content.res.AssetManager; – kafan1986 Oct 28 '14 at 10:50
  • Of course you should! I think that your IDE will do it well. There are a lot of ways to provide class with context, but i recommend you to put context as param to function: public String getFile(Context context, String fileName) – Tpec1k Oct 28 '14 at 10:55