Hong
Hong

Reputation: 18501

Is it possible to read a raw text file without Context reference in an Android library project

I could put a text file in folder res\raw of a library project, but reading it seems to require a Context reference. Could anyone shed some light on this?

Upvotes: 17

Views: 17606

Answers (3)

Debdutta Panda
Debdutta Panda

Reputation: 198

It is possible to get raw file without context in 2021

val Int.rawString: String
    get(){
        var res= ""
        val `is`: InputStream = Resources.getSystem().openRawResource(this)
        val baos = ByteArrayOutputStream()
        val b = ByteArray(1)
        try {
            while (`is`.read(b) !== -1) {
                baos.write(b)
            }
            res = baos.toString()
            `is`.close()
            baos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return res
    }

Upvotes: 1

yorkw
yorkw

Reputation: 41126

Check out my answer here to see how to read file from POJO.

Generally, the res folder should be automatically added into project build path by ADT plugin. suppose you have a test.txt stored under res/raw folder, to read it without android.content.Context:

String file = "raw/test.txt"; // res/raw/test.txt also work.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);

I did this before with an old SDK version, it should work with latest SDK as well. Give it a try and see if this helps.

Upvotes: 26

Radu Dan
Radu Dan

Reputation: 553

In order to access a resource you need a context. See here the definition of Context.class from the developer.android site

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

So, through a context you can access a resource file. You can create another class and pass the context from an activity to it. Create a method that reads a specified resource file.

For example:

public class ReadRawFile {
    //Private Variable
    private Context mContext;

    /**
     * 
     * Default Constructor
     * 
     * @param context activity's context
     */
    public ReadRawFile(Context context){
        this.mContext = context;
    }

    /**
     * 
     * @param str input stream used from readRawResource function
     * @param x integer used for reading input stream
     * @param bo output stream
     */
    private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){
        //not hitting end
        while(x!=-1){
            //write to output buffer
            bo.write(x);
            try {
                //read next
                x = str.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 
     * @return output file to string
     */
    public String readRawResource(){
        //declare variables
        InputStream rawUniversities = mContext.getResources().openRawResource(R.raw.universities);
        ByteArrayOutputStream bt = new ByteArrayOutputStream();
        int universityInteger;

        try{
            //read/write
            universityInteger = rawUniversities.read();
            writeBuffer(rawUniversities, universityInteger, bt);

        }catch(IOException e){
            e.printStackTrace();
        }
        //return string format of file
        return bt.toString();
    }

}

Upvotes: 3

Related Questions