Can I download an SQLite db on /sdcard and access it from my Android app?

前端 未结 5 1726
春和景丽
春和景丽 2020-11-30 18:38

I already found out that there is no way to bundle files in an .apk and have them on /sdcard, the best option so far being to download the large files upon first run. I came

相关标签:
5条回答
  • 2020-11-30 19:14

    Try this:

    String dir = Environment.getExternalStorageDirectory().getPath()
    File dbfile = new File(dir+"/mydb.sqlite"); 
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    System.out.println("Its open? "  + db.isOpen());
    
    0 讨论(0)
  • 2020-11-30 19:27

    Just an idea:

    • you can put your database.db into the assets folder.

    • if your database.db file is larger than 2Mb the system is unable to compress it, so you need other one options

    • you can rename your database.db for example database.jit or database.mp3 - which are not compressed, than at the first run you can rename it to database.db

    0 讨论(0)
  • 2020-11-30 19:37

    Sure you can. The docs are a little conflicting about this as they also say that no limitations are imposed. I think they should say that relative paths are to the above location and dbs there ARE private. Here is the code you want:

    File dbfile = new File("/sdcard/mydb.sqlite" ); 
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    System.out.println("Its open? "  + db.isOpen());
    
    0 讨论(0)
  • 2020-11-30 19:37

    check this out ...

    storing android application data on SD Card

    0 讨论(0)
  • 2020-11-30 19:39

    I share next code. Declare opcionesMenu Vector<String>

    Vector < String > opcionesMenu = new Vector< String >();
    // the database is SDCard. I saw  the code to Blackberry (net.rim)
    String rutaDB = "/storage/extSdCard/Android/databases/Offshore.db";
    
    
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(rutaDB, null);
    
        Cursor cursor = db.rawQuery("SELECT Codigo, Nombre FROM Ruta ORDER BY Codigo, Nombre", null);
    
        if (cursor.moveToFirst())
        {
            do
            {
                opcionesMenu.add(cursor.getString(0) + " - " + cursor.getString(1));
            } while(cursor.moveToNext());
        }
    
        db.close();
    
    0 讨论(0)
提交回复
热议问题