Accessing wrong raw resource in Android

天涯浪子 提交于 2019-12-23 22:55:56

问题


I have the following code. My idea is in OnCreate, I'll populate some categories from a text file in /res/raw to my database.

First a tokenize the read file by lines (myCatToken), then each of these I split again to get the id and name.

For some reason, instead of reading rat.txt I'm getting a totally different file and I have no idea why. The file that is actually read does exist in the /res/raw folder, however its named different.

It seems that it has something to do with the resource it sends to readTxtFromRaw, however I don't see whats wrong with it.

Thanks

StringTokenizer myCatToken = new StringTokenizer(new String(readTxtFromRaw(R.raw.rat)));
while(myCatToken.hasMoreTokens())
{
    StringTokenizer myCatDataToken = new StringTokenizer(myCatToken.nextToken(), ",");
    String insertString = new String("insert into " + DATABASE_TABLE_CATEGORIES +
            " (" + KEY_CATEGORIES_CATID + ", " + KEY_CATEGORIES_NAME + ") values " + 
            " (" + myCatDataToken.nextToken() + ", '" + myCatDataToken.nextToken() + "')");
    db.execSQL(insertString);
}

For reference I include this method I'm using. mCtx is Context:

private String readTxtFromRaw(Integer rawResource) throws IOException
{
    InputStream inputStream = mCtx.getResources().openRawResource(rawResource);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i = inputStream.read();
    while (i != -1)
    {
        byteArrayOutputStream.write(i);
        i = inputStream.read();
    }
    inputStream.close();

    return byteArrayOutputStream.toString();
}

回答1:


Clean your Workspace by going to "Project->Clean...". So the whole project will be built again and the generated class your.package.R will be refreshed.




回答2:


Actually I closed Eclipse and reopened it and it works ok. Sheesh!



来源:https://stackoverflow.com/questions/5919479/accessing-wrong-raw-resource-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!