Sumodh Nair
Sumodh Nair

Reputation: 1676

SQLite database not working in Android 4.4

I am using SQLite database for my PhoneGap project.The database is getting populated on every other OS I have tested except Android 4.4.0+ .

The Code for accessing database is below :-

public class MathWhiz extends CordovaActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        super.loadUrl(Config.getStartUrl());
        SharedPreferences sp = getSharedPreferences("MYPREFS",
                Activity.MODE_PRIVATE);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // If no shared prefs exist, e.g. first install, it doesn't matter - the
        // following will return false as a default
        Boolean database_copied = sp.getBoolean("database_copied", false);    
        if (!database_copied) {
            try {
                String pName = this.getClass().getPackage().getName();

                this.copy("Databases.db", "/data/data/" + pName
                        + "/app_database/");
                this.copy("sample.db", "/data/data/" + pName
                        + "/app_database/myFile/");
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("database_copied", true);
                editor.apply();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    void copy(String file, String folder) throws IOException {

        File CheckDirectory;
        CheckDirectory = new File(folder);
        if (!CheckDirectory.exists()) {
            CheckDirectory.mkdir();
        }
        InputStream in = getApplicationContext().getAssets().open(file);
        OutputStream out = new FileOutputStream(folder + file);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);
        in.close();
        out.close();

    }

}

And this is how I am using the database :-

window.openDatabase("sampleDB", "1.0", "sample", 200000);

Can anybody please point out what updates I need to do in order to make it work on Android 4.4 + ? Thanks

Upvotes: 4

Views: 1696

Answers (2)

Akash Singh
Akash Singh

Reputation: 5241

try it.. its working well...

public boolean copyDataBaseFromAssets(Context c) throws IOException {

    if(android.os.Build.VERSION.SDK_INT >= 17)
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/"+ DATABASE_NAME;
        else
            DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"+DATABASE_NAME;

        String pathToDatabaseFileInAssetFolder = DATABASE_NAME;
        String pathToDatabaseFileInSystem = DB_PATH;


this.getReadableDatabase(); 

> getReadableDatabase funcation used in db import code

        AssetManager assetManager = c.getResources().getAssets();
        InputStream inputStream = null;

        try {

            inputStream = assetManager.open(pathToDatabaseFileInAssetFolder);
        } catch (IOException ex) {

            return false;
        }

        if (inputStream != null) {

            OutputStream outputStream = new FileOutputStream(pathToDatabaseFileInSystem);

            byte[] buffer = new byte[1024];
            int length;

            while ((length = inputStream.read(buffer)) > 0) {

                outputStream.write(buffer, 0, length);
            }

            outputStream.flush();
            outputStream.close();
            inputStream.close();

            Log.d(TAG, "Database is copied");
            return true;
        }

        return false;
    }

Upvotes: 2

Zohra Khan
Zohra Khan

Reputation: 5302

Change this

    this.copy("Databases.db", "/data/data/" + pName + "/databases/");
    this.copy("sample.db", "/data/data/" + pName + "/databases/");

Upvotes: 0

Related Questions