views:

1366

answers:

3

In J2ME, I've do this like that: getClass().getResourceAsStream("/raw_resources.dat");

But in android, I always get null on this, why?

+4  A: 

For raw files, you should consider creating a raw folder inside res directory and then call getResources().openRawResource(resourceName) from your activity.

Samuh
Samuh, thanks for your reply, if i do this, i've got errors Android XML Format Problem, because i use not standard xml, is there other way to use it as really raw data?
Arkaha
what do you mean by non-standard XML? You can bundle XML files in assets also.
Samuh
Samuh, i'm porting j2me application, and it has files with extension *.xml, content of such files very similar to xml, but not the same, eclipse shouting on this files, because it can't compile it. probably i can set some kind of "ignore" on this files? but i don't know how..
Arkaha
Files in res/raw are not processed in any way by the resource compiler, so will not generate errors because of their content.
hackbod
+3  A: 

InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));

Adrian Vintu
A: 

TextView txtvw = (TextView)findViewById(R.id.TextView01); txtvw.setText(readTxt());

private String readTxt() { InputStream raw = getResources().openRawResource(R.raw.hello);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try
    {
        i = raw.read();
        while (i != -1)
        {
            byteArrayOutputStream.write(i);
            i = raw.read();
        }
        raw.close();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block

        e.printStackTrace();
    }


    return byteArrayOutputStream.toString();

}

TextView01:: txtview in linearlayout hello:: .txt file in res/raw folder (u can access ny othr folder as wel)

Ist 2 lines are 2 written in onCreate() method

rest is to be written in class extending Activity!!

poojan9118