简体   繁体   中英

Exception while using SQLiteAssetHelper class

I am trying to use SQLiteAssetHelper to copy my pre-populated db into android app. I have Android-7 on my device(Nvidia Shield & Motorola Phone). From Settings->App->MyApp I have enabled Calender , Location & Storage permissions

I have following permission in Manifest file as well

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

When I keep my db in assets/databases/databases folder, I get following exception:

Caused by: com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Unable to write /data/user/0/com.funcheap.funmapsf/databases/databases/MySFfuncheapDatabase.db to data

When I keep my db in assets/databases folder, I get following exception:

Caused by: com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/databases/MySFfuncheapDatabase.db file (or .zip, .gz archive) in assets, or target folder not writable

Unfortunately, this SO question doesnt help me

As @miket said if you reference the internal path Android will use that reference I had this issue and this was my very sloppy test code NOTE commented out call

public void getINTERNAL(){
    Context context = this;
    File file = new File(context.getFilesDir().getAbsolutePath());
    String NT = String.valueOf(file);
    THE_PATH = NT+"/";
    System.out.println("INTERNAL PATH =========> "+THE_PATH);
}


public void getAvailable(){

    //getINTERNAL();

    String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED) && (!state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))) {

        File removable = ContextCompat.getExternalFilesDirs(this, null)[1];
        THE_PATH = String.valueOf(removable);
        if(THE_PATH.length() > 4) {
            //THE_PATH = THE_PATH.substring(0, THE_PATH.length() - 5);
            THE_PATH = THE_PATH + "/Documents/";
            System.out.println("new path ====> "+THE_PATH);
        }
        System.out.println("EXTERNAL PATH ====> " + THE_PATH);
        System.out.println("Internal Path used if EXTERNAL PATH = NULL ");
        //getINTERNAL(); DO NOT CALL HERE YOU WILL CHANGE THE_PATH TO internal
        // no need to set THE_PATH for internal storage it will default on its own
        // WHY DOES THIS HAPPEN THE_PATH variable is PUBLIC STATIC hence it is GLOBAL
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM