0

I tried to save with path context.getExternalFileDirs(), but it saved files to folder /storage/9016-4EF8/Android/data/package/files on SDcard.

File file = new File(context.getExternalFilesDir(null), myFolder);

I searched Android only support read/write on folder of App like/Android/data/package/files, but I want to save the file to specific folder like /storage/9016-4EF8/MyFolder. How can I achieve this?

Young King
  • 79
  • 2
  • 13
  • Can you add the piece of code that you are using for this? – havabon Jan 15 '18 at 08:43
  • That is already the app specific folder on sd card. So that should be writable. The rest of the sd card is not writable using the file system. – greenapps Jan 15 '18 at 08:47
  • I need a solution save data to specific folder. Because this way there's a problem with Data on /Android/data/package/files , it will be removed when user update app or uninstall/reinstall . – Young King Jan 15 '18 at 08:55
  • Yes i know. Not with an update. Only at uninstall. Now read my comments and answer so you know what you have to do. – greenapps Jan 15 '18 at 09:00
  • Hi @greenapps I'm not sure about case user update application . Are you sure app data not remove with updating ? – Young King Jan 15 '18 at 09:08
  • Yes 100% sure. And how easy to test ;-). – greenapps Jan 15 '18 at 09:09

3 Answers3

1

If you want to write to the whole micro SD card then use Storage Access Framework.

Use Intent.ACTION_OPEN_DOCUMENT_TREE to let the user of your app choose the card.

On Android 7+ have a look at Storage Volumes.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Very nice if you have a example about writing data using Storage Access Framework – Young King Jan 15 '18 at 09:03
  • 1
    Just google a bit. There are so many examples using this intent. There is also Intent.ACTION_OPEN_DOCUMENT and Intent.ACTION_CREATE_DOCUMENT. Try them all. – greenapps Jan 15 '18 at 09:06
  • 1
    @YoungKing Posted Answer to your Question Post Comment you you want more help full code can be explained at my web-site with a tutorial androidstackoverflow.com – James_Duh May 11 '18 at 01:50
1

Here is a link to that explains how to let the user select INTERNAL or EXTERNAL storage. You let the user select the path and maintain that static variable and all data will be written to the SQLite DB using the THE_PATH variable

LINK

James_Duh
  • 1,321
  • 11
  • 31
0

You can use Environment.getExternalStorageDirectory() this will give you the public external directory to read and write files.

sample code to create a text file in /storage/9016-4EF8/MyFolder/test.txt

File docsFolder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!docsFolder.exists()) {
   docsFolder.mkdir();
}
File file = new File(docsFolder.getAbsolutePath(),"test.txt"); 

Edit:

public static String getExternalSdCardPath() {
    String path = null;

    File sdCardFile = null;
    List<String> sdCardPossiblePath = Arrays.asList("external_sd", "ext_sd", "external", "extSdCard");

    for (String sdPath : sdCardPossiblePath) {
            File file = new File("/mnt/", sdPath);

            if (file.isDirectory() && file.canWrite()) {
                    path = file.getAbsolutePath();

                    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
                    File testWritable = new File(path, "test_" + timeStamp);

                    if (testWritable.mkdirs()) {
                            testWritable.delete();
                    }
                    else {
                            path = null;
                    }
            }
    }

    if (path != null) {
            sdCardFile = new File(path);
    }
    else {
            sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    }

    return sdCardFile.getAbsolutePath();

}

Resources:

https://gist.github.com/PauloLuan/4bcecc086095bce28e22 https://www.codeproject.com/Questions/716256/find-path-of-external-SD-card

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • No. getExternalStorageDirectory() will give a path to external memory. Not to a removable micro sd card. So the path you put so nice in bold you will never get. – greenapps Jan 15 '18 at 09:04
  • But it's NOT SD card (removable) . I tested this code . Environment.getExternalStorageDirectory() return Internal Storage – Young King Jan 15 '18 at 09:05
  • my bad I didn't see the removal part, I have added some sources which may be useful. thanks – vikas kumar Jan 15 '18 at 09:27