It is not possible to access Android assets (and for me there are no posts or tutorials), please help!

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{
    //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 also tried to access it from MainActivity:

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 am I doing wrong? Is the folder with my assets located correctly? Are my files copied? Is my code placed correctly?

+3
source share
2 answers

To fix it:

" ( , AndroidManifest.xml)

/nbproject/project.properties

* ( ) *

assets.dir = assets.available =

/nbproject/build-impl.xml,

"if = assets.available" ,

Thats it - ":///android_asset/" .

, !!...

http://blog.yetisoftware.com/2009/04/02/android-netbeans-and-the-assets-directory/

+3

, :

  • .
  • ( drectories).

: assets -> app -> image, assets -> app -> another dir. app dir asssete.

0

Source: https://habr.com/ru/post/1786384/


All Articles