Android og persistens developer.android/guide/topics/data/data-storage.html

Post on 31-Dec-2015

25 views 1 download

description

Android og persistens http://developer.android.com/guide/topics/data/data-storage.html. Your data storage options are the following: Shared Preferences Store private primitive data in key-value pairs. Internal Storage Store private data on the device memory. - PowerPoint PPT Presentation

Transcript of Android og persistens developer.android/guide/topics/data/data-storage.html

Android og persistenshttp://developer.android.com/guide/topics/data/data-storage.html

• Your data storage options are the following:• Shared Preferences Store private primitive data in key-

value pairs. • Internal Storage Store private data on the device

memory. • External Storage Store public data on the shared

external storage. • SQLite Databases Store structured data in a private

database. • Network Connection Store data on the web with your

own network server.

SharedPreferences

Skriv til SharedPreferences

// We need an Editor object to make preference changes.// All objects are from android.context.Context•SharedPreferences settings = getSharedPreferences("MyPrefsFile", 0);•SharedPreferences.Editor editor = settings.edit();•editor.putBoolean("silentMode", mSilentMode); 

Læs fra SharedPreferences

•SharedPreferences settings = getSharedPreferences("MyPrefsFile", 0);•boolean silent = settings.getBoolean("silentMode", false);•setSilent(silent);

Tekstfiler i Android (java)

Skriv på tekstfil•FileOutputStream outStream = openFileOutput("myfiletext",Context.MODE_PRIVATE); •PrintWriter outTextStream = new PrintWriter(outStream); •outTextStream.println("En tekst");•outTextStream.close();

Læs fra tekstfil•FileInputStream inFileStream = openFileInput("myfiletext"); •InputStreamReader inReader = new InputStreamReader(inFileStream); •BufferedReader buffReader = new BufferedReader(inReader); •String text;•text = (String) buffReader.readLine();•buffReader.close();

Serialisering af java-ObjekterFor at serialisere skal man implementere SerializableKlassen skal desuden forsynes med en unique ID

•import java.io.Serializable;

•public class BusinessModel implements Serializable {•private static final long serialVersionUID = -2255373015564100242L;•…………•}

 

Filer og java-Objekter

Skriv objekt til fil•FileOutputStream outFileStream = openFileOutput("myfileobjects”,Context.MODE_PRIVATE); •ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);•String simpleObject = "Hej "+(i+1); •outObjectStream.writeObject(simpleObject);•outTextStream.close();

Læs objekt fra fil•FileInputStream inStream = openFileInput("myfileobjects"); •ObjectInputStream inObjectStream = new ObjectInputStream(inStream); •String text;•String simpleObject;•simpleObject = (String) inObjectStream.readObject();•inObjectStream.close();

Check for exception EOFException

public directories on the external storage

• Music/ - Media scanner classifies all media found here as user music.

• Podcasts/ - Media scanner classifies all media found here as a podcast.

• Ringtones/ - Media scanner classifies all media found here as a ringtone.

• Alarms/ - Media scanner classifies all media found here as an alarm sound.

• Notifications/ - Media scanner classifies all media found here as a notification sound.

• Pictures/ - All photos (excluding those taken with the camera).

• Movies/ - All movies (excluding those taken with the camcorder).

• Download/ - Miscellaneous downloads.