简体   繁体   中英

Issue when exporting database file to external storage

try{
    File dbFile = context.getDatabasePath(context.getString(R.string.database_name));
    FileInputStream inputStream = new FileInputStream(dbFile);

    String outputFileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + dbFile.getName();
    OutputStream outputStream = new FileOutputStream(outputFileName);

    byte[] buffer = new byte[1024];
    int length;
    while((length = inputStream.read(buffer))>0){
        outputStream.write(buffer, 0, length);
    }

    outputStream.flush();
    outputStream.close();
    inputStream.close();
}
catch (Exception e){
    e.printStackTrace();
}

I am using this code to export my applications database to a devices downloads folder on external storage. I am able to successfully export my application database to the downloads folder and re-export it, however, if I delete the exported file from the downloads folder using the devices default file manager I am no longer able to export the database file as I get this error: java.io.FileNotFoundException: /storage/emulated/0/Download/APPLICATION_DATABASE: open failed: EEXIST (File exists) .

So far this issue seems to only occur on my Samsung galaxy s10e running android 11 as I have tested for this issue on the android studio emulator using android 11 and android 9 and am unable to reproduce it.

The media store has scanned your file.

De default file manager deleted the file without informing the media store.

At creating the file the OS checkes the media store for existing files.

And the media store will say yes then.

You first have to delete that media store entry

Query the media store for the uri of the file.

Then delete the entry using that uri.

Pretty sloppy of that manager.. ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM