Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to access Android assets (and no post or tutorial works for me), please help!
    primarykey
    data
    text
    <p>I'm facing a kind of curse about asset accessing with Android. I'm using NetBeans 6.9.1 and testing on a 2.3 Android Virtual Device. I'm trying to access a database file ("database.db") stored in the assets folder (though I've had to make the directory by myself because it didn't even existed in my project folder) and I'm simply unable to do it after more than 3 days lost looking for a solution. Here a summary of my tragic process:</p> <ul> <li>I've created a directory called "assets" in my NetBeans project directory (just where the "res" or the "src" folders are).</li> <li>I've copied the "database.db" file inside and even a "sample.txt", which I've also stored in the "assets/sub" subdirectory as I'm testing everything humanly possible.</li> <li><p>I have a class that inherits from "SQLiteOpenHelper" where I'm simply trying to access my database file and, after seeing that it's nearly impossible, I'm just trying to see what my assets folder contains:</p> <pre><code>public class DataBaseHelper extends SQLiteOpenHelper{ private static String DB_PATH = "/data/data/org.me.androidbt/databases/"; private static String DB_NAME = "database.db"; private SQLiteDatabase myDataBase; private final Context myContext; public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } private void myFunction() throws IOException{ //Firs try, explodes terribly at the first line InputStream is = myContext.getAssets().open("sub/sample.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; int number = 0; while ((line = br.readLine()) != null) { // Just used to check the "line" value when debugging number++; } br.close(); // Second try, didn't explodes AssetManager mngr = myContext.getResources().getAssets(); String[] str = mngr.list(""); // String only contains three elements: "images", "sounds" and "webkit" // (an there aren't such things in my assets folder) // Real try to open the DB, dies horribly and blowns up my PC InputStream myInput = myContext.getAssets().open(DB_NAME); </code></pre></li> </ul> <p>I've also tried to access it from the MainActivity itself:</p> <pre><code>try{ AssetManager mng = this.getAssets(); String[] str = mng.list(""); // The same three elements... // And this explodes in a nuclear toxic cloud that kills a thousand birds InputStream is = this.getAssets().open("sub/sample.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; int number = 0; while ((line = br.readLine()) != null) { number++; } br.close(); }catch(Exception e){ // Life is sad... } </code></pre> <p>And now, some questions: What I'm doing wrong? Is my assets folder correctly placed? Are my files correclty copied? Is my code correctly placed?</p>
    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.
    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