从SD卡访问Sqlite DB会出错

时间:2018-04-20 06:39:29

标签: android sqlite android-sdcard sd-card sqliteopenhelper

我在SD卡中有一个sqlite数据库。我试图从活动中打开它,但得到以下错误。

Failed to open database '/storage/sdcard1/deltalearn/deltalearn.db'.
                                                      android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
                                                          at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                          at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:214)
                                                          at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:198)
                                                          at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                          at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)

以下是我用来打开数据库的代码。我无法使用Environment.getExternalStorageDirectory(),因为这指向少数手机中的内部存储空间。

DatabaseHandler databaseHandler = new DatabaseHandler(this);
public DatabaseHandler(Context context) {
super(context, AndroidUtils.getExternalStoragePath()
        + File.separator + Constants.DATABASE_FOLDER_NAME
        + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
Log.d("DatabaseHandler::","public DatabaseHandler(Context context)");

File dbFile=new File(AndroidUtils.getExternalStoragePath()
        + File.separator + Constants.DATABASE_FOLDER_NAME
        + File.separator + DATABASE_NAME);

SQLiteDatabase.openOrCreateDatabase(dbFile, null);

Log.d("DatabaseHandler::", "path: route:" + AndroidUtils.getExternalStoragePath()
        + File.separator + Constants.DATABASE_FOLDER_NAME
        + File.separator + DATABASE_NAME);
}

public static String getExternalStoragePath() {
String removableStoragePath = "";
//working in Moto, but not working in Samsung S3
File fileList[] = new File("/storage/").listFiles();
for (File file : fileList) {
    if (!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead()) {
        removableStoragePath = file.getAbsolutePath();
    }
}
Log.d("AndroidUtils:: ", "getExternalStoragePath: removableStoragePath:" + removableStoragePath);
if (removableStoragePath.contains("emulated")) {
    //working in Samsung s3, but not working in Moto
    String path = "";
    File file = new File("/system/etc/vold.fstab");
    FileReader fr = null;
    BufferedReader br = null;

    try {
        fr = new FileReader(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        if (fr != null) {
            br = new BufferedReader(fr);
            String s = br.readLine();
            while (s != null) {
                if (s.startsWith("dev_mount")) {
                    String[] tokens = s.split("\\s");
                    path = tokens[2]; //mount_point
                    if (!Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)) {
                        break;
                    }
                }
                s = br.readLine();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fr != null) {
                fr.close();
            }
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Log.d("AndroidUtils::", "getExternalStoragePath: path:" + path);
    return path;
} else {
    //working in Moto, but not working in Samsung galaxy S3
    return removableStoragePath;
}
}

但是如果将Db复制到内部存储器,则相同的Db可以正常工作。

1 个答案:

答案 0 :(得分:0)

您应该验证是否使用了getWriteableDatabase() 此外Environment.getExternalStorageDirectory()并不总是指向SD卡,它也可能指向内部存储器,因设备而异,因此您无法真正依赖它。据我所知,没有一种方法或方法可以随时获得SD卡。这可能就是为什么它不适用于某些设备。为什么不将数据库存储在内部存储中?只有您的应用可以访问它的位置。如果您将图像保存为blob,则可以存储它们的路径。