0

I have two different memory locations in android. One is the internal memory location called the external sd card and the other one is the removable memory card. I have got the read and write permissions in the manifest. I am also checking its availability. I have also added android:requestLegacyExternalStorage="true" My problem is that I am not able to write to this removable media. When I try to write I get this error.

2020-06-06 20:54:13.334 29148-29148/com.test.debug I/OMD::SDCard: /storage/8E14-3919 is removable external storage
2020-06-06 20:54:13.332 29148-29148/com.test.debug W/sdatabase.debug: type=1400 audit(0.0:8919357): avc: granted { read open } for pid=29148 path="/mnt/media_rw/8E14-3919" dev="mmcblk1p1" ino=1 scontext=u:r:untrusted_app:s0:c74,c258,c512,c768 tcontext=u:object_r:vfat:s0 tclass=dir
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err: java.io.FileNotFoundException: /storage/8E14-3919/freebookslibrary.db: open failed: EACCES (Permission denied)
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:496)
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at com.khan.mybookslibrary.BackUpExternal.exportDatabase(BackUpExternal.java:145)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at com.khan.mybookslibrary.BackUpExternal.access$100(BackUpExternal.java:57)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at com.khan.mybookslibrary.BackUpExternal$4.onClick(BackUpExternal.java:115)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at android.os.Looper.loop(Looper.java:213)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8147)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:     at libcore.io.Linux.open(Native Method)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8015)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:482)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err:    ... 12 more
2020-06-06 20:54:13.406 29148-29212/com.test.debug W/libEGL: EGLNativeWindowType 0x6f62540b90 disconnect failed

> The code with which am trying to achieve is as follows:

private void exportDatabase(String fileName) {
    final File dbFile = context.getDatabasePath(DATABASE_NAME);//existing database file path

    SDCard sdc = new SDCard();
    File file = sdc.findSdCardPath(context);

    FileChannel source      = null;
    FileChannel destination = null;

    if(file!= null){
        String backUpPath  = file.getAbsolutePath() + "/" + fileName + ".db";
        try {
            source = new FileInputStream(dbFile).getChannel();
            destination = new FileOutputStream(backUpPath).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            alert(context.getResources().getString(R.string.Database_exported_external));
        } catch (IOException e) {
            e.printStackTrace();
            alert(context.getResources().getString(R.string.Failed_export_database_external));
        }
    }
    else
        return;
}

I need a solution to this problem

Dilazak
  • 139
  • 1
  • 1
  • 11
  • https://stackoverflow.com/a/57653808/588532 – Uriel Frankel Jun 06 '20 at 16:08
  • @Uriel Frankel, that is not a solution. That is a hack. A hack that does not even work for this problem. – blackapps Jun 07 '20 at 07:29
  • Use the path of the second item returned by getExternalFilesDirs to write to your app specific directory on sd card. Or use SAF to be able to write to the whole card. – blackapps Jun 07 '20 at 07:34
  • @blackapps i did not understand .. can you please explain a bit or write the statement as a reference – Dilazak Jun 07 '20 at 12:36
  • File dirs[] = getExternalFilesDirs(); The second item is on the sd card mostly. So use dir[1] as the directory for your file. – blackapps Jun 07 '20 at 13:55
  • @blackapps you suggested File dirs[] = getExternalFilesDirs() but it is asking for a string argument? – Dilazak Jun 07 '20 at 17:16

0 Answers0