AmirrezASe7eN AmirrezASe7eN - 1 month ago 18
Android Question

Error using openOrCreateDatabase in android (Could not open database)

I'm trying to create a database from within a class. The app stops working on run on Api 22+ and I get this error in logcat:

Unable to create application android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

The code:

public class G extends Application {

public static SQLiteDatabase database;
public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE = DIR_SDCARD + "/DO_NOT_DELETE/";

public void onCreate() {

new File(DIR_DATABASE).mkdirs();
database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/database.sqlite", null);


I have no problem running it on Android versions that are lower than 5. I also added the required permission in AndroidManifest.xml. What could be the problem?

Answer Source

Oops, I made a dumb mistake. I tried to create/open a database on an emulator that didn't have a sdcard storage. Now I gave it some space for sdcard and it's working fine.