1 / 18

Acessing file

Acessing file. Cource : Embedded S ystems and Realtime Instructor: Dr. Tran Thi Minh Chau Student : Nguyen Van Lan Email: lancan.nguyen@gmail.com. Out line:. Data Storage & Accessing File Shared Preferences Internal Storage External Storage

burian
Download Presentation

Acessing file

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Acessing file • Cource: Embedded Systems and Realtime • Instructor: Dr. Tran Thi Minh Chau • Student : Nguyen Van Lan • Email: lancan.nguyen@gmail.com

  2. Out line: Data Storage & Accessing File Shared Preferences Internal Storage External Storage SQLite Databases Custom Format Text ( My Project) HTML & Spanned Demo

  3. Data Storage • Trong android, Data đượclưutrữvớinhiều option khácnhau: Shared Preferences Dữliệulà private, dướidạngcặp Key-Value Internal Storage Lưudữliệu ở chếđộ private vàlưutrêncácthiếtbịvậtlý External Storage Lưudữliệu ở chếđộ public vàlưutrêncácthiếtbịvậtlýngoàinhưSdcard SQLite Databases Làhìnhthứclưudữliệucócấutrúcvàhỗtrợtruyvấntheocâulệnh SQL Network Storage JDBC

  4. Shared Preferences Làhìnhthứclưutrữdạng <Name, Value> Dữliệulàkiểucơbản: booleans, floats, ints, longs, and strings. Hiệuquảtrongviệclưuthông tin cấuhình, thông tin kếtnối, ứngdụng…. Cógìđógiốngvới Bundles Bềnvữnghơn Bundles

  5. Shared Preference API • Tạomộtđốitượng Shared Preferences • getShared Preferences() • getPreferences() • Đểthaotác: • Gọi edit() trảvềđốitượngSharedPreferences.Editor • Thêmgiátrị : putBoolean(),putString() • Commit() đểthựchiệnthaotác • Thamkhảothêm :http://developer.android.com/reference/android/content/SharedPreferences.html

  6. Internal Storage • File đượclưutrựctiếptrênthiếtbịbộnhớtrong.(Internal Storage) • Dữdiệunàylà private vàchỉchịusựquảnlícủachínhứngdụngđangchạynó, cácứngdụngkháckhôngthểtruycậpđược • Tạoghi private file trong Internal Storage: • DùngopenFileOutput() phươngthứcnàytrảvềđốitượngFileOutputStream: • Code : FileOutputStream f = openFileOutput( FILENAME, Context.MODE_PRIVATE); f.write(string.getBytes()); // ghi file f.close(); // đóngtreamlại

  7. Khiđọc file từbộnhớtrong ta dùngphươngthứcopenFileInput() đểtạomộtđốitượngFileInputStream • Sauđóthaotácvới file bằngcácphươngthức: read(),close() FileInputStream.read() Tip : Những file thườngxuyênsửdụngtrongquátrìnhchạyứngdụng ta cầnlưuchúngcốđịnhtạimộtnơi : res/raw OpenRawResource() -> Lưu cache files: Tăngtốcđộchochươngtrình, tuynhiênphảithườngxuyêndọndẹp file cache đểtốiưukhônggiannhớtrong

  8. External Storage • Lưutrữngoài ( External Storage ) Ưuđiểm: Khônggianlưutrữlớn Dễ di chuyểngiữacácthiếtbị :PC, Mobile Dễthaythếnângcấp Nhượcđiểm: Tínhbảomậtkhôngcao : public data Tốcđộtruycậpthấp

  9. Accessing file on External Storage • Trướckhidùng External Storages cầnphảikiểmtratrạngtráithiếtbị booleanmExternalStorageAvailable = false;booleanmExternalStorageWriteable = false;String state = Environment.getExternalStorageState();if (Environment.MEDIA_MOUNTED.equals(state)) {    // We can read and write the mediamExternalStorageAvailable = mExternalStorageWriteable = true;} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {    // We can only read the mediamExternalStorageAvailable = 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 writemExternalStorageAvailable = mExternalStorageWriteable = false;}

  10. Accessing • Mởmột file: getExternalFilesDir() (API >= 8) getExternalStorageDirectory() (API < 7) Thưmựcđểviếtdữliệu: /Android/data/<package_name>/files/ <package_name>: tên package địnhdạngtheokiểutrong java Lưu ý : cầnphảikhaibáoquyềntruycậptrong manifest <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> Thaokhảo : http://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfu

  11. Custom Fomat text

  12. Spanned & HTML CharSequence Immutable markup, immutable text Mutable markup, immutable text Immutable markup, immutable text IntgetSpanStart(Object) IntgetSpanEnd(Object) T[] getSpans(intstart,intend,class<T>) Void removeSpan(Object) Void setSpan(Object, int,int, int flags) Editable insert(intwhere,Charsequence) Editable replace(int, int, Charsequence) Editable delete(int ,int) Void clear() Void clearSpans() Spanned SpannedString Spannable SpannableString Editable SpannableStringBuilder Interface Implementation

  13. Presentation and saving Presentation Saving String Html.toHtml(Spanned) Biểudiễnđốitượng Spanned bằngmã HTML Đếnđây ta cóthểlưuđốitượngvào CSDL vàlầnsaugọilại Spanned Html.fromHtml(String) Đổimộtđoạnmã HTML sang mộtđốitượng Spanned Edittext.setText(Spanned) Biểudiễn Spanned rabằngmộtTextView

  14. Ngườidùngthaotácvới Edit text Saving Convert to Html Html.toHtml Spanned String Database Convert to Spanned String Html.fromHtml Spanned Presentation

  15. But ….. • Khôngphảithẻ HTML nàocũnghỗtrợbởiphươngthứcHtml.fromHtml, Html.toHtml • Ta phải Implement interface Html.TagHandler Public Class myHandler implements TagHandler{ public void handleTag(booleanopening,String tag, Editable output,XMLreaderxmlReader){ // doing somethings with tag } } Khisửdụng Spanned Html.fromHtml(String source, ImageGetter null, myHandler handler); Ở phầnnày ta thamkhảo ở: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.java

  16. Conclusion • Phầnđầutiên Accessing File làkỹthuậtcơbảntrongpháttriểnứngdụng Android • Trongphần Custom format text làgiảiphápcánhânemđưaratrongquátrìnhlàm Project choứngdụngNhậtký Reference: File : Android-Chapter15-Files.pptx [Cleverland University] http://www.ibm.com/developerworks/vn/library/x-androidstorage/index.html http://developer.android.com/guide/topics/data/data-storage.html http://developer.android.com/reference/android/text

  17. Thank you!

More Related