我的开发人员应用程序在Android 9上打开数据库的问题 [英] My developer app problem with opening database on Android 9

查看:83
本文介绍了我的开发人员应用程序在Android 9上打开数据库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我吗?

我已经在包含数据库的Google Play商店上创建并发布了该应用程序,我已经在不同的Android手机上对其进行了测试.除了在Android 9上,我的应用程序在android版本(Android 10、8、5等)上均可完美运行,您知道如何解决该问题吗?我在Huawei Smart Z,Honor P20,Samsung Y6,Xiaomi Mi上发现了这个问题.Play商店控制台表示我的应用可以在该模型上运行.我是应用程序开发的初学者.

I have created and published the app on Google play store that contains a database, I have tested it on different android phones. My app works perfect on android version (Android 10,8,5 etc...) except on Android 9, do you know how can I fix that problem? I find that problem on Huawei Smart Z, Honor P20, Samsung Y6,Xiaomi Mi. Play store console says that my app can run on that models. I am beginner in app development.

这在我的华为Smart上发生:当我安装该应用程序时,当我想输入数据库时​​它会打开并崩溃,而当我再次输入该应用程序时,它会崩溃并在logcat上崩溃:

This happening on mine Huawei Smart: when I install the app it opens and crashes when I want to enter in database, and when I want to enter in app again it crashes and on logcat:

E/Path 1: data/data/com.gema/databases/ 
E/SQLiteLog: (14) cannot open file at line 36906 of [c255889bd9]
E/SQLiteLog: (14) os_unix.c:36906: (2) open(//data/data/com.gematranslate/databases/gema_recnik.db) - 
E/SQLiteDatabase: Failed to open database 'data/data/com.gematranslate/databases/gema_recnik.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (Sqlite code 14 SQLITE_CANTOPEN): Could not open database, (OS error - 2:No such file or directory)

我在android清单中放了:

In android manifest I put:

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

这是我的检查数据库代码:

This is my code for check db:

public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
        myContext.getDatabasePath(DB_NAME).getPath();
        checkDB.disableWriteAheadLogging();
    } catch (SQLException e)
    {
        //
    }
    if (checkDB!= null){
        checkDB.close();
    }
    return checkDB != null? true : false;

这是我打开数据库的代码:

This is my code for opening db:

public void openDataBase() {
    String myPath = DB_PATH + DB_NAME;
    myContext.getDatabasePath(DB_NAME).getPath();
    db = SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READWRITE);
    db.disableWriteAheadLogging();

如果有人可以帮助我,非常感谢.

If anyone can help me, thanks a lot.

推荐答案

这是所有版本的Android都支持的解决方案

This is a solution that is supported on all versions of Android

public class DataBase extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static String DATABASE_PATH = "";
private static String LAST_DATABASE_PATH = "";
private SQLiteDatabase mDataBase;
private final Context ctx;

public DataBase2(@Nullable Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getPath();
    this.ctx = context;

    if (!checkIfDBExists()) {
        createDataBase();
    }

    File dbFile = new File(Environment.getDataDirectory().getPath()+"/data/"+ BuildConfig.APPLICATION_ID +"/databases");
    if (dbFile.isDirectory()) {
        String[] child = dbFile.list();
        assert child != null;
        for (int i = 0; i < child.length; i++) {
            if (!(child[i].equals("database.db") || child[i].equals(DATABASE_NAME))) {
                new File(dbFile, child[i]).delete();
            }
        }
    }
}

public void createDataBase() {
    this.getReadableDatabase();
    this.close();
    try {
        copyDataBase();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {
        File DbFile = ctx.getDatabasePath(DATABASE_NAME);
        if (DbFile.exists()) {
            copyDataBase();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private boolean checkIfDBExists() {
    File dbFile = new File(DATABASE_PATH);
    return dbFile.exists();
}



public void openDataBase() {
    close();
    mDataBase = SQLiteDatabase.openDatabase(DATABASE_PATH, null, CREATE_IF_NECESSARY);
}

private void copyDataBase() throws IOException {
    InputStream mInput = ctx.getAssets().open("database.db");
    String outfileName = DATABASE_PATH;
    OutputStream mOutput = new FileOutputStream(outfileName);
    byte[] buffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(buffer)) > 0) {
        mOutput.write(buffer, 0, mLength);
    }
    mOutput.flush();
    mInput.close();
    mOutput.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}


@Override
public synchronized void close() {
    if (mDataBase != null)
        mDataBase.close();

    SQLiteDatabase.releaseMemory();
    super.close();
}

}

这篇关于我的开发人员应用程序在Android 9上打开数据库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆