I am facing some kind of scourge regarding access to products with Android. I am using NetBeans 6.9.1 and testing on 2.3 Android Virtual Device. I am trying to access the database file ("database.db") stored in the resources folder (although I had to create the directory myself because it did not even exist in my project folder), and I just could not do it after more than 3 days lost in finding a solution. Here is a brief overview of my tragic process:
- I created a directory called "assets" in my NetBeans project project (only where the "res" or "src" folders are located).
- I copied the file "database.db" inside and even "sample.txt", which I also saved in the subdirectory "assets / sub", since I am testing everything humanly.
I have a class that inherits from "SQLiteOpenHelper", where I just try to access my database file, and, seeing that it is almost impossible, I just try to see what my resources folder contains: / p>
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{
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) {
number++;
}
br.close();
AssetManager mngr = myContext.getResources().getAssets();
String[] str = mngr.list("");
InputStream myInput = myContext.getAssets().open(DB_NAME);
I also tried to access it from MainActivity:
try{
AssetManager mng = this.getAssets();
String[] str = mng.list("");
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){
}
And now, some questions: what am I doing wrong? Is the folder with my assets located correctly? Are my files copied? Is my code placed correctly?
source
share