Your SlideShare is downloading. ×
0
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
[Curs Android] C09 - Stocarea Datelor (IPW 2011)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

[Curs Android] C09 - Stocarea Datelor (IPW 2011)

866

Published on

These slides are part of the Android Development Course which I taught to 10 students, while participating in the IP Workshop Summer School 2011. …

These slides are part of the Android Development Course which I taught to 10 students, while participating in the IP Workshop Summer School 2011.
http://www.scoaladevara.info/2011/?page_id=39

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
866
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  1. Android Stocarea Datelor Vlad PETRE vlad@vladpetre.com http://twitter.com/vladpetre88 http://facebook.com/vladpetre88 http://www.linkedin.com/in/vladpetre881 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  2. Date Predefinite • ReadOnly • Assets – directorul assets – reprezinta un depozit nestructurat de fisiere • Resurse – directorul res/raw – devin R.raw.nume_resursa2 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  3. Assets • Ca sa deschidem un asset: AssetManager am = context.getResources().getAssets(); InputStream is = am.open (String assetName); • Ca sa obtin lista de asseturi: String[] assetsList = am.list (String path); • path – este relativ la directorul assets • Ex: pentru a cauta direct in directorul assets, atunci path = “”;3 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  4. Resurse • InputStream is = context.getResources().open(int id); • id este un int de forma – R.raw.nume_resursa4 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  5. Preferinte (Setari)5 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  6. Preferinte (Setari) • Mecanism prin care putem retine anumite date de configurare (ex: numele utilizatorului, ringtone-ul, etc). • Putem stoca cinci tipuri de date: – String – float – boolean – int – long • Datele persista atata timp cat aplicatia e instalata in telefon.6 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  7. Scrierea unor preferinte (1) String KEY = “myPrefs”; int mode = Context.MODE_PRIVATE; SharedPreferences myPrefs = context.getSharedPreferences (KEY, mode); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString(“name”, “Alex Marcu"); prefsEditor.putInt(“age”, 23); prefsEditor.commit();7 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  8. Scrierea unor preferinte (2) • Moduri: – Context. MODE_PRIVATE • Default. Fisierul poate fi accesat doar de catre aplicatia care l-a creat. – Context.MODE_APPEND • Scrierea are loc la sfarsitul fisierului. Fisierul poate fi accesat doar de catre aplicatia care l-a creat. – Context.MODE_WORLD_READABLE • Permitem tuturor aplicatiilor sa citeasca fisierul respectiv. – Context.MODE_WORLD_WRITEABLE • Permitem tuturor aplicatiilor sa scrie fisierul respectiv.8 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  9. Citirea unor preferinte String key = “myPrefs“; int mode = Context.MODE_PRIVATE; SharedPreferences myPrefs = context.getSharedPreferences (key, mode); String name = myPrefs.getString(“name”, "nothing"); int age = myPrefs.getInt(“age”, -1);9 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  10. PreferenceActivity Context.MODE_PRIVATE10 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  11. PreferenceActivity • Activitate specializata pentru setari. • Din punct de vedere vizual, este construita automat. • Preferintele trebuiesc definite intr-un fisier *.xml plasat in directorul res/xml. • addPreferencesFromResource(R.xml.xml_setari); • Tipuri de preferinte: – String – boolean11 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  12. Fisierul xml (1) • Contine elemente de tipul Preference – ListPreference, CheckBoxPreference, EditTextPreference, etc • Atribute posibile: – key – cheie unica de identificare – title – un titlu informativ pentru user – summary – un sumar informativ pentru user – defaultValue – valoarea initiala – dependency – determina disponibilitatea setarii in functie de o alta12 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  13. Fisierul xml (2) • Tipuri de setari: Nodul radacina din cadrul – ListPreference fisierului xml se numeste – CheckBoxPreference PreferenceScreen. – EditTextPreference – RingtonePreference • Grupate in diferite categorii – PreferenceCategory • key • title13 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  14. Exemplu cu PreferenceActivity • Definim un fisierul XML • Scriem PreferenceActivity • Definim PreferenceActivity in Manifest14 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  15. Fisierul XML Salvat in res/xml/settings.xml15 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  16. Siruri cu valori Salvat in res/values/arrays.xml16 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  17. PreferenceActivity17 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  18. Citirea setarilor SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String name = preferences.getString("name", "(no name)"); boolean isStudent = preferences.getBoolean("student", false); int year = Integer.parseInt(preferences.getString("year", "1"));18 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  19. Stocarea interna19 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  20. Stocare Interna • Sub forma de fisiere aflate intr-un director special in cadrul aplicatiei. • In general, stocarea de fisiere are loc in memoria interna nevolatila, nu pe cardul SD.20 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  21. Scrierea unui fisier intern String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = context.openFileOutput (FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); • FILENAME nu are voie sa contina o cale, ci se precizeaza direct numele fisierului.21 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  22. Citirea unui fisier intern String FILENAME = "hello_file"; FileInputStream fis = context.openFileInput(FILENAME); • FileInputStream se foloseste exact ca in Java SE. • FILENAME nu are voie sa contina o cale, ci se precizeaza direct numele fisierului.22 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  23. Listarea fisierelor interne String[] fileList = context.fileList(); • Intoarce o lista cu numele fisierelor interne (fara cale).23 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  24. Stergerea fisierelor interne String FILENAME = "hello_file"; boolean successfullyDeleted = context.deleteFile(FILENAME) • FILENAME nu are voie sa contina o cale, ci se precizeaza direct numele fisierului.24 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  25. Directoare interne File dir = context.getDir(String dirName, int mode); • Metoda imi intoarce directorul respectiv. Daca acesta nu exista, il creaza. • Se foloseste exact ca in Java SE.25 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  26. Cache Intern File cacheDir = context.getCacheDir(); • Un director special. • Se sterge automat atunci cand sistemul decide ca are prea putina memorie interna.26 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  27. Stocare externa27 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  28. Stocare externa • Are loc pe cardul SD, unde pot scrie toate aplicatiile oriunde. • Trebuie sa avem grija, deoarece cardul SD nu este permanent prezent (avem mai multe stari). • Odata cu API 8 au fost introduse cateva directoare speciale pe cardul SD.28 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  29. Starile cardului SD • String Environment.getExternalStorageState(); – MEDIA_MOUNTED – MEDIA_MOUNTED_READ_ONLY – MEDIA_CHECKING – MEDIA_REMOVED – MEDIA_SHARED – MEDIA_UNMOUNTABLE – MEDIA_UNMOUNTED – MEDIA_BAD_REMOVAL – MEDIA_NOFS29 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  30. Exemplu verificare stare card SD boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; }30 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  31. Cum stocam datele pe cardul SD API <= 7 API >= 8 Date private Date private Date partajate Date partajate Cache extern Cache extern • In general, calea catre cardul SD este de forma: o /sdcard/31 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  32. Date private32 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  33. Date private conform API <= 7 • Preluam calea catre cardul SD: – File externalDir = Environment.getExternalStorageDirectory(); • Iar apoi scriem fisierele in directorul de pe card: – /Android/data/<package_name>/files/ • Ceea ce stocam conform acestei metode este sters la dezinstalare, doar daca aplicatia a fost instalata pe Android 2.2 (API >= 8)!33 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  34. Date private conform API >= 8 (1) • Calea catre cardul SD o preluam: – File externalDir = Environment.getExternalFilesDir (String type); • Cu parametrul type putem preciza un subdirector: – DIRECTORY_MUSIC – DIRECTORY_PODCASTS – DIRECTORY_RINGTONES – DIRECTORY_ALARMS –…34 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  35. Date private conform API >= 8 (2) • Daca type este null, atunci primesc directorul radacina al locatiei unde este recomandat ca eu sa salvez fisiere pe cardul SD. • Ceea ce stocam conform acestei metode este sters la dezinstalare. • Subdirectoarele ajuta Scannerul Media al Android-ului. Spre exemplu, sunetele stocate in DIRECTORY_RINGTONES nu vor aparea in player-ul de muzica.35 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  36. Date partajate36 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  37. Date partajate conform API <= 7 (1) File externalDir = Environment. getExternalStorageDirectory (); • Intoarce directorul radacina al cardului SD.37 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  38. Date partajate conform API <= 7 (2) • Se recomanda sa salvam fisierele corespunzator urmatoarelor subdirectoare: – Music/ – Podcasts/ – Ringtones/ – Alarms/ – Notifications/ – Pictures/ – Movies/ – Download/38 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  39. Date partajate conform API >= 8 File externalDir = Environment.getExternalStoragePublicDirectory (String type); • Intoarce directorul radacina extern unde atat noi, cat si alte aplicatii, putem stoca fisiere. • Cu parametrul type putem preciza un subdirector: – DIRECTORY_MUSIC – DIRECTORY_PODCASTS – DIRECTORY_RINGTONES – DIRECTORY_ALARMS – …39 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  40. Cache extern40 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  41. Cache extern conform API <= 7 File externalDir = Environment. getExternalStorageDirectory (); • Intoarce directorul radacina al cardului SD. • Iar apoi scriem fisierele in directorul de pe card: – /Android/data/<package_name>/cache/ • Sistemul NU va sterge aceste fisiere daca ramane fara spatiu pe cardul SD (deci le gestionam singuri). • Ceea ce stocam conform acestei metode este sters la dezinstalare, doar daca aplicatia a fost instalata pe Android 2.2 (API >= 8)!41 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  42. Cache extern conform API >= 8 File externalCacheDir = Environment.getExternalCacheDir(); • Fisierele sunt sterse automat daca userul dezinstaleaza aplicatia. • Sistemul NU va sterge aceste fisiere daca ramane fara spatiu pe cardul SD (deci le gestionam singuri).42 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  43. Referinte • http://pdm.ipworkshop.ro • http://developer.android.com/reference/android/conten • http://www.kaloer.com/android-preferences • http://developer.android.com/guide/topics/data/data-st43 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  44. Întrebări?44 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea

×