1

I have a very specific issue - I am trying to write to external storage on an Asus Nexus 7, but it is writing to the emulated directory on the device.

Here is the code I am using:

public static void writeExternalMedia(Context context) {
    if(isExternalStorageWritable()) {
        String content = "hello world";

        File filedir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test");
        filedir.mkdir();

        File file;

        FileOutputStream outputStream;

        try {
            file = new File(filedir, "test.txt");

            if (!file.exists()) {
                file.createNewFile();
            }

            outputStream = new FileOutputStream(file);
            outputStream.write(content.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

Whenever I restart the device, the directories appear under the device when plugged in, which is what I would expect to happen when the function above gets executed.

I have tried searching for a solution and cannot find the answer to my question.

eplewis89
  • 112
  • 11
  • I'm pretty sure it's the same as this: http://stackoverflow.com/questions/37946892/files-copied-by-my-app-not-showing-up-on-pc – Budius Jun 23 '16 at 15:47
  • I utilized that methodology and it did not work, unfortunately. Thanks for the effort though! – eplewis89 Jun 23 '16 at 15:52

2 Answers2

1

I made two methods. One for creating a file and one for appending to it. I think the issue is that you're not calling createNewFile.

private File CreateFile(String fileName)
{
    File file = new File(this.getFilesDir(), fileName);
    try
    {
        if(!file.exists())
        {
            file.createNewFile();
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return file;
}

private void appendToFile(String file, String content)
{
    try
    {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput(file, this.MODE_APPEND));
        outputStreamWriter.append(content + "\n");
        outputStreamWriter.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
Ali Reza Dehdar
  • 1,991
  • 1
  • 15
  • 17
  • Thanks for trying, but that didn't work. The file directory that I am trying to utilize resides on the external storage. The actual directory being written to is reported as "/storage/emulated/0/Download/test". When I write a file to this emulated directory, which works flawlessly, the file does not appear until the device restarts. – eplewis89 Jun 23 '16 at 16:00
0

Alright after much searching and testing I finally came across a solution, linked via one of the other answers.

https://stackoverflow.com/a/28448843/979220

The solution was to scan the media files, which causes the files to propagate to the external storage, rather than staying in the emulated storage.

Community
  • 1
  • 1
eplewis89
  • 112
  • 11