0

I'm searching how to create a file in sd card. Currently I have this code :

public class Store {

private final String fileName = "histoires.json";

public void createFile(Context context, String json) {
    File file = new File (getDir(context), fileName);
    try {
        FileOutputStream out = new FileOutputStream(file);
        out.write(json.getBytes());
        out.flush();
        out.close();

    } catch (Exception e) {
        Log.e("Erreur enregistrement du fichier", e.toString());
    }
}

private File getDir(Context context) {
    File dir;
    if(!isExternalStorageReadable() || !isExternalStorageWritable()) {
        dir = context.getFilesDir();
    }
    else {
        dir = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
    }
    return dir;
}

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
            Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}}

In my activity I just call this :

new Store().createFile(this, json);

Problem is : I see my file but always in phone memory. I used getExternalFilesDir(). Anyway it doesn't want to save my file in SD card. I checked content of my dir variable in getDir() at return statement and it is : /storage/emulated/0/Android/data/com.histoire_horreur/files/Documents

Can you please help me ?

Harshiv
  • 376
  • 2
  • 17
Rom1
  • 346
  • 4
  • 17
  • If you are trying to write to externalStorage -e.g. Memory/SD Card, you should check [this](http://stackoverflow.com/q/22219312/2952723) – Harshiv Jun 09 '16 at 06:04
  • When I use System.getenv("SECONDARY_STORAGE") I have this result : /storage/sdcard1. It seems good but first it doesn't show all the path to Android/data/com.histoire_horreur/files/Documents, and second it does not save anything in SD Card... Maybe sdcard1 is wrong finally ? – Rom1 Jul 03 '16 at 08:22
  • Android does not implement any standard method for accessing external SDcard/Memory card, it is implemented by device manufacturer,so path to external SDcard and method to get it varies with devices. At some place it is `/storage/sdcard1` (e.g. in some Sony Experia devices), and somewhere else it may be `/storage/extsdcard` or something else. It also varies with android API level. So you need to figure it out separately. Are you trying to write in memory cards directory : `Android/data/data/your.package/files/` ? – Harshiv Jul 04 '16 at 18:44
  • Yes I am. How to figure it out separately ? – Rom1 Jul 05 '16 at 19:35
  • There are already many questions similar to yours on SO. For example, you can check [this](http://stackoverflow.com/questions/18050388/get-external-sdcard-location-in-android) or [this one](http://stackoverflow.com/questions/16637935/a-safe-path-to-external-storage/16638064#16638064) or just search "external SDcard android" on SO and you'll probably find a working solution. – Harshiv Jul 07 '16 at 02:53

1 Answers1

0

Try using :

String mfileName;
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
  • Instead of going to the right folder, it goes in : /storage/emulated/0/histoires.json. And it's always in phone memory – Rom1 May 29 '16 at 10:11