Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing wrong raw resource in Android
    primarykey
    data
    text
    <p>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.</p> <p>First a tokenize the read file by lines (myCatToken), then each of these I split again to get the id and name.</p> <p>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.</p> <p>It seems that it has something to do with the resource it sends to readTxtFromRaw, however I don't see whats wrong with it.</p> <p>Thanks</p> <pre><code>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); } </code></pre> <p>For reference I include this method I'm using. mCtx is Context:</p> <pre><code>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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload