Android Internal and External Storage difference

This post assumes that you have understanding of standard file input/output APIs in java.io.
When working with files in Android, first you need to be aware of Internal and External File storage.

Android devices have two file storage areas: “internal” and “external” storage. During initial days, internal storage means the memory which is built into device, and external storage means external devices such as SD Card which gets mounted to Android device. But these days, even the built in memory has internal and external storage as well. Below are differences between Internal and external Storage

Internal storage:
It’s always available.
Files saved here are accessible by only your app by default.
When the user uninstalls your app, the system removes all your app’s files from internal storage.
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

External storage:
It’s not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
It’s world-readable, so files saved here may be read outside of your control.
When the user uninstalls your app, the system removes your app’s files from here only if you save them in the directory from getExternalFilesDir().
External storage is the best place for files that don’t require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.


import android.app.Activity;
import android.os.Bundle;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.io.*;

public class MyActivity extends Activity {

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

        writeToFile(getApplicationContext(), "aaaaaaaaaaaa bbbbbbbbbbb kjuhiuh");

        String file_content = readFromFile();

        System.out.println("Content of file is :"+file_content);

        Log.i("ccccccccccContent of file is :", file_content);

    }

    private void writeToFile(Context ctx,String data) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(ctx.openFileOutput("config.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }

    private String readFromFile() {

        String ret = "";

        try {
            InputStream inputStream = openFileInput("config.txt");

            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString);
                }

                inputStream.close();
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e("login activity", "Can not read file: " + e.toString());
        }

        return ret;
    }

}

Download Android File Example Source Code