Internal storage methods

Let's begin with internal storage mechanisms on Android. For those with experience in standard Java programming, this section will come pretty naturally. Internal storage on Android simply allows you to read and write to files that are associated with each application's internal memory. These files can only be accessed by the application and cannot be accessed by other applications or by the user. Furthermore, when the application is uninstalled, these files are automatically removed as well.

The following is a simple example of how to access an application's internal storage:

public class InternalStorageExample extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // THE NAME OF THE FILE
        String fileName = "my_file.txt";
        // STRING TO BE WRITTEN TO FILE
        String msg = "Hello World.";
        try {
            // CREATE THE FILE AND WRITE
            FileOutputStream fos = openFileOutput(fileName, 
             Context.MODE_PRIVATE);
            fos.write(msg.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here we simply use the Context class's openFileOutput() method, which takes as its first argument the name of the file to be created (or overridden) and as its second argument the visibility of that file (just like with SharedPreferences, you can control the visibility of your files). It then converts the string we want to write to byte form and passes it into the output stream's write() method. One thing to mention though is an additional mode that can be specified with openFileOutput() and that is:

  • MODE_APPEND: This mode allows you to open an existing file and append a string to its existing contents (any other mode and the existing contents will be deleted)

Furthermore, if you are programming in Eclipse, then you can go to the DDMS screen and look at your application's internal files (amongst other things):

Internal storage methods

And so we see the text file that we just created. For those developing with the terminal, the path for this would be /data/data/{your-app-path}/files/my_file.txt. Now, unfortunately, reading back files is much more verbose and the code fo how you would do that looks like:

public class InternalStorageExample2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // THE NAME OF THE FILE
        String fileName = "my_file.txt";
        try {
        // OPEN FILE INPUT STREAM THIS TIME
            FileInputStream fis = openFileInput(fileName);
            InputStreamReader isr = new InputStreamReader(fis);
            // READ STRING OF UNKNOWN LENGTH
            StringBuilder sb = new StringBuilder();
            char[] inputBuffer = new char[2048];
            int l;
            // FILL BUFFER WITH DATA
            while ((l = isr.read(inputBuffer)) != -1) {
                sb.append(inputBuffer, 0, l);
            }
            // CONVERT BYTES TO STRING
            String readString = sb.toString();
            Log.i("LOG_TAG", "Read string: " + readString);
            // CAN ALSO DELETE THE FILE
            deleteFile(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here we start by opening a file input stream instead and pass it into a stream reader. This will allow us to call the read() method and read in the data as bytes which we can then append to a StringBuilder. Once the contents have been read back fully, we simply return the String from the StringBuilder and voila! At the end, just for the sake of completeness, the Context class provides you with a simple method for deletingfiles saved in the internal storage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181