test1990bdvt
test1990bdvt

Reputation: 39

Not getting the package name

I'm trying to retrieve the file from the raw folder dynamically the code as below

try{
        DataInputStream dataIO= new DataInputStream(getResources().getIdentifier("raw/"+chapter, null ,<what to write>);
        String strLine= null;
        while((strLine = dataIO.readLine())!=null){
            buffer.append(strLine);
            buffer.append("\n");
        }
        dataIO.close();
    }catch(Exception e){}

If I type the package name directly in the "what to write" section, it showing the error. Please give some idea about it.

Upvotes: 1

Views: 175

Answers (2)

Mani
Mani

Reputation: 196

A fully qualified resource name is of the form "package:type/entry", which is missing in your code. I use the below code to access raw resource.

getResources().getIdentifier("package:type/entry", null, null);

Upvotes: 0

Rajesh
Rajesh

Reputation: 15774

Assuming that you want to retrieve the id of a raw resource dynamically, the following code snippet would be useful:

int id = getResources().getIdentifier(chapter, "raw", getPackageName());

and your DataInputStream object should be constructed thus:

DataInputStream dataIO= new DataInputStream(getResources().openRawResource(id));

Upvotes: 2

Related Questions