..


Sponsored Links

Data Storage: Internal and External Storage Storage

So far we have shown the reader a very broad and thorough overview rigarda as the development of applications on the Android platform. At this point the guide the reader will be able to use the elements shown to create applications of medium complexity. But what the reader is missing, and that's what we will explain in this chapter and the next, is the way in which you can save some data permanently (when we have to do with storing data permanently usually uses the term Data Storage).

For example, you may need to save preferences of the user-entered on a permanent basis and to allow the application, while starting to read these preferences and act accordingly. Android offers several options for saving data permanently and is the programmer's responsibility to use the most appropriate mode as needed.

We are now going to present the way that Android offers.

Internal Storage

It 'a very simple way in which we will create a file, typically a text file, in which we can write and read some data. By default, this type of saving is a saving private, which means that the file can be read and written only by the application that created it and is therefore not possible to access it from another application.
Let's see now, with these few lines of code, a practical example of creating a file:






 String fileName = "file_di_prova";







 String string = "This is a file written by MrWebMaster!";









 FileOutputStream fos = openFileOutput (fileName, Context.MODE_PRIVATE);







 fos.write (string.getBytes ());







 fos.close ();



This code simply declare two string variables, one for the name of the file and the file contents. It then opens a stream to the file, the writing is done with the write method and finally closes the stream with the close method.

If we need to change the defualt properties set for the visibility of the file (as we have said that providing access to only applucazione that created the file), we must act on the second parameter of the method openFileOutput. And 'it can choose the following values:

  • Context.MODE_WORLD_READABLE: is allowed read access to all applications.
  • Context.MODE_WORLD_WRITEABLE: is allowed write access to all applications.

In summary this method of storage is a very simple and intuitive, widely used in applications because of its easy implementation.

External Storage

This type of data storage allows the saving of some data, by application, on removable device such as an SD card. Files stored on this type of media is defined world-readable file that is granted to all applications to read it.

It 'should point out to the reader the "dangers" of using this method of storage. This is because if you save data to an external storage you should check that the saved data is not essential for the proper functioning of the application. This is because you have no control over this data when the user removes the SD card, insert it into a computer and accidentally delete some parts of it.

Therefore we recommend the use of this methodology in an economical storage.

Help develop applications for Android
E-Learning
Flash MX and ActionScript (Course) Flash MX and ActionScript (Course)
Become a developer of Web sites from 29 €.
HTML (Course) HTML (Course)
The markup language for the Web from 29 €.
Java (Course) Java (Course)
OOP Programming in Java SUN. From 49 €.
Sponsored Links