Android write to internal storage

Stefano Sandonà picture Stefano Sandonà · Jul 4, 2015 · Viewed 13.1k times · Source

I tried to store a *.m4a file (a microphone recording) into a "public" directory, I mean into a directory like the ones of Telegram, WhatsApp accessible from the "My Files" app. I've tried to create my own directory:

File directory = new File(Environment.getDataDirectory()
            + "/myDir/");

    if (!directory.exists()) {
        directory.mkdir();
    }

But the directory wasn't create. I need to store files into a directory accessible when I plug my smartphone to the PC. What's wrong? Could someone help me?

Answer

pri picture pri · Jul 4, 2015

Try using one or more of the following:

1) Make sure you have WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml

2) Use mkdirs() function

3) Use Environment.getExternalStorageDirectory() and try writing it in external storage