android-studioandroid-external-storageandroid-studio-3.1android-internal-storageandroid-studio-3.1.3

Saving Files in Android - For Beginners (Internal / External Storage)


I'm very new to android development and I need a beginners help please...

I am creating an app that should have 2 directories, a Databases directory and an Images directory, just like Whatsapp has.

I want to see those directories in the File Manager when I'm browsing my phone, just like you see other apps folders. I tried everything I found here, to create it in the Internal storage and in the External storage from code. The folders seem to be created, but when I browsed my phone's File Manager, I couldn't find the folder with my app name and inside it my Databases and Images folders... What am I doing wrong? do I need to create those folders in the android studio as adding folders? or do I need to create it from code?

Can you please give me the actions I need to do to accomplish this task? I'm using android studio 3.1.3. Thanks for the helpers! :)


Solution

  • The terms "Internal Storage" and "External Storage" might be confusing at first, because Google's intentions are different from what we would expect & know from our day-to-day use of language: "External" doesn't necessarily mean the "SD Card". This guy made a great article about the terminology confusion

    According to your intentions, you'd want to be working with the External Storage concept. The differences are well explained in the Documentation, but I'll shortly brief them to you here.

    At the end I'll provide you an example, but first lets know the basics:

    Internal Storage

    External Storage


    So now that we know you need External Storage, there are several things needed to be done before starting:

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

    Each permission stands by its own, meaning you don't need to have both if, for example, you only wish to read files instead of writing them


    Example Time!

    In the given method, we will save a text file inside the root directory.

    Credits to this article

    public void writeFileExternalStorage() {
    
        //Text of the Document
        String textToWrite = "bla bla bla";
    
        //Checking the availability state of the External Storage.
        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
    
            //If it isn't mounted - we can't write into it.
            return;
        }
    
        //Create a new file that points to the root directory, with the given name:
        File file = new File(getExternalFilesDir(null), filenameExternal);
    
        //This point and below is responsible for the write operation
        FileOutputStream outputStream = null;
        try {
            file.createNewFile();
            //second argument of FileOutputStream constructor indicates whether
            //to append or create new file if one exists
            outputStream = new FileOutputStream(file, true);
    
            outputStream.write(textToWrite.getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    I'd like to answer specifically to some of your questions:

    do I need to create those folders in the android studio as adding folders? or do I need to create it from code?

    Definitely not via the Android Studio. These are your projects folder, containing your code. The way to do it is mentioned above.

    I couldn't find the folder with my app name and inside it my Databases and Images folders... What am I doing wrong?

    Probably saved your files as Internal Storage ones / saved them as project folders as you mentioned earlier - and those wouldn't (and shouldn't) show up.


    Useful things to know

    There are 2 types of directories: public and private.

    Private

    Example: the WhatsApp directory (in my phone) is located right at the root level. Calling it would be: getExternalFilesDir("WhatsApp/...")

    Public (Downloads/Movies/Images libraries)

    Example: getting the Documents folder would look like: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)