Reputation: 31
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:
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:
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);
I've also tried to access it from the MainActivity itself:
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...
}
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?
Upvotes: 3
Views: 7188
Reputation: 1
i had this problem and i suggest you check couple of things:
For example: assets -> app -> image
, assets -> app -> another dir
. In this example the app dir is the only dir in the asssete root.
Upvotes: 0
Reputation: 285
To fix:
Create a directory called ‘assets’ at your project root (same dir that AndroidManifest.xml lives)
In /nbproject/project.properties, change
*add these two lines (it wont exist)*
assets.dir=assets assets.available=true
In /nbproject/build-impl.xml,
there is line in the “if=assets.available” target that reads
that needs to be changed to
That’s it – you should be all set and “file:///android_asset/” should be accessible to your application and contain the contents of your project’s assets directory.
Got this link and it worked for me!!...
http://blog.yetisoftware.com/2009/04/02/android-netbeans-and-the-assets-directory/
Upvotes: 2