Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access Android assets (and no post or tutorial works for me), please help!

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've created a directory called "assets" in my NetBeans project directory (just where the "res" or the "src" folders are).
  • 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.
  • 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?

like image 311
Johanovski Avatar asked Dec 05 '25 16:12

Johanovski


1 Answers

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/

like image 123
Balaji Avatar answered Dec 08 '25 08:12

Balaji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!