/ / Prístup k databáze Sqlite DB z karty SD dáva chybu - android, sqlite, android-sdcard, sd-card, sqliteopenhelper

Prístup k Sqlite DB z karty SD poskytuje chybu - android, sqlite, android-sdcard, sd-card, sqliteopenhelper

Na karte SD mám sqlite db. Snažím sa to otvoriť z aktivity, ale zobrazuje sa mi chyba uvedená nižšie.

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)

Nižšie je uvedený kód, ktorý používam na otvorenie databázy DB. Nie som schopný použiť Environment.getExternalStorageDirectory() pretože to ukazuje na interné úložisko v niekoľkých telefónoch.

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;
}
}

Ale rovnaký Db funguje, ak je skopírovaný do interného úložiska.

odpovede:

0 pre odpoveď č. 1

Mali by ste si overiť, že používate getWriteableDatabase().
tiežEnvironment.getExternalStorageDirectory() nemusí vždy smerovať na kartu SD, môžeukážte tiež na interné úložisko, líši sa to od zariadenia po zariadenie, takže sa na neho nemôžete spoľahnúť. Pokiaľ viem, neexistuje žiadny určitý spôsob alebo metóda, ako vždy získať kartu SD. A to je pravdepodobne dôvod, prečo to tak nie je na niektorých zariadeniach nepracujem. Prečo neuložiť svoju databázu do interného úložiska? Kde k nej má prístup iba vaša aplikácia. Ak ukladáte obrázky ako blob, môžete namiesto toho uložiť ich cesty.