1

I save my database in the folder /storage/emulated/0/Download/Database. Initially, the database functions properly. However, when I uninstall and then reinstall the application, I encounter an error while attempting to access the database: /storage/emulated/0/Download/DATABASE/tablet_pos.db (OS Error: Permission denied, errno = 13). I have verified that the file exists in the correct folder with the correct file name. The 'download/database' folder contains two files: 'tablet_pos.db' and 'tablet_pos.journal'.

My question is: How can I utilize the same database stored in the 'download/database' folder when reinstalling the app?

Future<void> initDatabase() async {
if (Platform.isAndroid) {
  databasesPath = '/storage/emulated/0/Download/DATABASE';

  // storageDirectory = await getExternalStorageDirectory();
  // databasesPath = p.join(storageDirectory.path, FolderNames.database);
}

if (!await Directory(databasesPath).exists()) {
  await Directory(databasesPath).create(recursive: true);
}
var path = p.join(databasesPath, 'tablet_pos.db');
print('databasesPath $databasesPath $path');

var databaseFile = File(path);

if (!await databaseFile.exists()) {
  try {
    print('await $database $path');
    await databaseFile.create(recursive: true);
  } catch (e) {
    print('Error creating database file: $e');
    return;
  }
}

// Check if the database file exists
if (!await databaseFile.exists()) {
  print('Database file not found');
  return;
}

print('try $database $path');
// Attempt to read the file to verify accessibility
try {
  await databaseFile.readAsBytes();
} catch (e) {
  print('Database file is not readable: $e'); // error here on reinstall
  return;
}

database = await openDatabase(path, version: 1,
    onCreate: (Database db, int version) async {
  await db.execute(CustomerDB.createTable);

});
print('database $database $path');

}

draw134
  • 1,053
  • 4
  • 35
  • 84
VDTe
  • 438
  • 1
  • 4
  • 17
  • 1
    seems like a permissions issue. see https://stackoverflow.com/questions/60558577/how-to-solve-os-error-permission-denied-errno-13-in-flutter – griffins Jun 07 '23 at 10:37
  • Target sdk version is 33 and permission is already granted. Added external read and write in manifest – VDTe Jun 08 '23 at 02:00

0 Answers0