0

I'm using an HTC Desire 610 for Android development. I followed the examples in other answers to the same question, but all my file saves end up on internal device storage, and not the SD card. The phone definitely recognizes the SD card, and from within the phone I can transfer files to the SD card.

I tried hard-coding the file path to the SD card, and I also tried running the app while not plugged into the computer - all to no avail. It is extremely important that my app be able to save these files directly to the SD card. Any ideas?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    writeDataToFile("2394872934729348");

}


public void writeDataToFile(String data){
    BufferedWriter bufferedWriter = null;

    String dataOutFileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/data.txt";

    try{
        bufferedWriter = new BufferedWriter(new FileWriter(dataOutFileName,true));
        bufferedWriter.write(data);
        bufferedWriter.flush();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    finally {
        try {
            if(bufferedWriter != null){
                bufferedWriter.close();
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    MediaScannerConnection.scanFile(this,new String[]{dataOutFileName}, null,null);
}
}

EDIT: I'm now trying this. I get a FileNotFoundException unless I write directly to the package file (com.mypackage etc)

    File dataDir = new File("/storage/ext_sd/Data/");
    dataDir.mkdirs();
    File outputFile = new File(dataDir,"data.txt");

    try{
        FileOutputStream fos = new FileOutputStream(outputFile);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos);
        outputStreamWriter.write(data);
        outputStreamWriter.close();

    }
    catch (IOException e){
        Log.e("Exception", "File write failed: " + e.toString());
    }
emhomm4
  • 212
  • 1
  • 9
  • 2
    There is nothing in your code that has anything to do with [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). You are writing to what the Android SDK refers to as [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html). Use `getExternalFilesDirs()` to get locations that your app can write to on external and removable storage. – CommonsWare Apr 12 '17 at 15:28

2 Answers2

2

To access the private storage directory on the external storage, use getExternalFilesDirs(), not getExternalStorageDirectory().

It will return an array with entries each location. The first entry in the array is considered the primary external storage and you should use that location unless it's full or unavailable. Refer Using the External Storage on Android API guides for detailed description.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • Thanks, this worked! Is it not possible to make my own directory, or do I have to write to my app's package name directory? – emhomm4 Apr 12 '17 at 16:16
  • 1
    You are welcome. Of course you can creare a directory. Check [this](http://stackoverflow.com/questions/2130932/how-to-create-directory-automatically-on-sd-card). – Bertram Gilfoyle Apr 12 '17 at 16:22
  • Every time I call "getExternalFilesDirs()" it only lists the files within my app's package directory on the SD card. And it won't let me mkdir() on the SD card unless it's in my package directory. – emhomm4 Apr 12 '17 at 17:13
  • 1
    Sorry for getting back to you late. To access top-level directories, you have to create a FILE object as the link in the previous comment says, not getExternalStorageDirectory() . – Bertram Gilfoyle Apr 12 '17 at 17:38
  • See my edit for the changes I made and the new issue. – emhomm4 Apr 12 '17 at 17:55
  • 1
    The path must be wrong then. Try printing getExternalStorageDirectory(). So that you can verify it. – Bertram Gilfoyle Apr 12 '17 at 18:01
  • The path ends up being /storage/ext_sd/Android/data/com.[packageName]/files – emhomm4 Apr 12 '17 at 18:02
  • The only other folder on the SD card is "DCIM" and I get a permission denied exception if I try to write in there. – emhomm4 Apr 12 '17 at 18:06
  • 1
    I so appreciate your help! Starting to think it's an issue with this phone in particular. – emhomm4 Apr 12 '17 at 18:22
0

Have you give to your app correct permissions in AndroidManifest.xml ? If you haven't done so already, you will need to give your app the correct permission to write to the SD Card by adding the line below to your Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Maybe that is the problem

JakSok
  • 124
  • 4