Accessing File

download Accessing File

of 18

Transcript of Accessing File

  • 7/31/2019 Accessing File

    1/18

    Acessing file

    Cource: Embedded Systems and Realtime

    Instructor: Dr. Tran Thi Minh Chau

    Student : Nguyen Van Lan

    Email: [email protected]

  • 7/31/2019 Accessing File

    2/18

    Out line:

    Data Storage & Accessing File

    Shared Preferences

    Internal Storage

    External Storage

    SQLite Databases

    Custom Format Text ( My Project)

    HTML & SpannedDemo

  • 7/31/2019 Accessing File

    3/18

    Data Storage

    Trong android, Data clutrvinhiu option khc nhau:Shared Preferences

    Dliu l private, didngcp Key-ValueInternal Storage

    Ludliu ch private v lu trn cc thitbvt lExternal Storage

    Ludliu ch public v lu trn cc thitbvt lngoi nh Sdcard

    SQLite Databases

    L hnh thcludliu c cu trc v htr truy vntheo cu lnh SQL

    Network Storage

    JDBC

  • 7/31/2019 Accessing File

    4/18

    Shared Preferences

    L hnh thclutrdng

    Dliu l kiucbn: booleans, floats, ints, longs, and strings.

    Hiuqu trong vic lu thng tin cu hnh, thng tin ktni,ngdng.

    C g gingvi Bundles

    Bnvnghn Bundles

  • 7/31/2019 Accessing File

    5/18

    Shared Preference API

    Tomtitng Shared Preferences

    getShared Preferences()

    getPreferences()

    thao tc:

    Gi edit() trvitng SharedPreferences.Editor

    Thm gi tr : putBoolean(),putString()

    Commit() thchin thao tc

    Tham kho thm:http://developer.android.com/reference/android/content/Sh

    aredPreferences.html

    http://developer.android.com/reference/android/content/SharedPreferences.htmlhttp://developer.android.com/reference/android/content/SharedPreferences.htmlhttp://developer.android.com/reference/android/content/SharedPreferences.htmlhttp://developer.android.com/reference/android/content/SharedPreferences.html
  • 7/31/2019 Accessing File

    6/18

    Internal Storage

    File clutrctip trn thitbbnh trong.(InternalStorage)

    Ddiu ny l private v chchusqun lca chnh ngdngangchy n, cc ngdng khc khng th truy cp

    c To ghi private file trong Internal Storage:

    Dng openFileOutput() phngthc ny trvitngFileOutputStream:

    Code : FileOutputStream f = openFileOutput( FILENAME,

    Context.MODE_PRIVATE);

    f.write(string.getBytes()); // ghi file

    f.close(); //ng tream li

  • 7/31/2019 Accessing File

    7/18

    Khi c file tbnh trong ta dng phngthcopenFileInput() tomtitng FileInputStream

    Sau thao tc vi file bng cc phngthc: read(),close()

    FileInputStream.read()

    Tip : Nhng file thng xuyn sdng trong qu trnh chyngdng ta cnlu chng cnhtimtni : res/raw

    OpenRawResource()

    -> Lu cache files:Tngtc cho chng trnh, tuy nhin phithng xuyn dndp file cache tiu khng gian nh trong

  • 7/31/2019 Accessing File

    8/18

  • 7/31/2019 Accessing File

    9/18

    External Storage

    Lutr ngoi ( External Storage )

    uim:

    Khng gian lutrln

    D di chuyngia cc thitb :PC, Mobile

    D thay thnng cp

    Nhcim:

    Tnh bomt khng cao : public data

    Tc truy cpthp

  • 7/31/2019 Accessing File

    10/18

    Accessing file on External Storage

    Trc khi dng External Storages cnphikim tra trng tri thitb

    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;

    }

  • 7/31/2019 Accessing File

    11/18

    Accessing

    Mmt file: getExternalFilesDir() (API >= 8)getExternalStorageDirectory() (API < 7)

    Thmcvitdliu:

    /Android/data//files/: tn package nhdng theo kiu trong

    java

    Lu : cnphi khai bo quyn truy cp trong manifest

    Thao kho : http://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfu

    http://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfuhttp://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfuhttp://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfuhttp://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfuhttp://www.youtube.com/watch?v=_WtTmPmr-zI&feature=relmfu
  • 7/31/2019 Accessing File

    12/18

    Custom Fomat text

  • 7/31/2019 Accessing File

    13/18

    Spanned & HTML

    Int getSpanStart(Object)

    Int getSpanEnd(Object)

    T[] getSpans(int start,int end,class)

    Void removeSpan(Object)

    Void setSpan(Object, int,int, int flags)

    Editable insert(int where,Charsequence)

    Editable replace(int, int, Charsequence)

    Editable delete(int ,int)

    Void clear()

    Void clearSpans()

    Immutable markup, immutable text

    Mutable markup, immutable text

    Immutable markup, immutable text

    Spanned SpannedString

    Spannable

    Editable SpannableStringBuilder

    SpannableString

    CharSequence

    Interface Implementation

  • 7/31/2019 Accessing File

    14/18

    Presentation and saving

    Presentation

    Spanned Html.fromHtml(String)

    imton m HTML sang mt

    itng Spanned

    Edittext.setText(Spanned)

    Biudin Spanned ra bngmt

    TextView

    Saving

    String Html.toHtml(Spanned)

    Biudinitng Spannedbng m HTML

    ny ta c thluitngvo CSDL v ln sau gili

  • 7/31/2019 Accessing File

    15/18

    Database

    Html.toHtml

    Html.fromHtml

    Ngi dng thao tcvi Edit text

    Convert to Html

    SpannedString

    String

    Spanned

    Presentation

    Saving

    Convert to Spanned

  • 7/31/2019 Accessing File

    16/18

    But ..

    Khng phith HTML no cnghtrbiphngthc Html.fromHtml,Html.toHtml

    Ta phi Implement interface Html.TagHandlerPublic Class myHandler implements TagHandler{

    public void handleTag(boolean opening,String tag, Editableoutput,XMLreader xmlReader){

    // doing somethings with tag

    }

    }Khi sdng Spanned Html.fromHtml(String source, ImageGetter null,myHandler handler);

    phn ny ta tham kho:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.java

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.javahttp://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.javahttp://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.javahttp://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.java
  • 7/31/2019 Accessing File

    17/18

    Conclusion

    Phnu tin Accessing File l kthutcbn trong phttrinngdng Android

    Trong phn Custom format text l gii php c nhn em a ratrong qu trnh lm Project cho ngdngNht k

    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

    http://www.ibm.com/developerworks/vn/library/x-androidstorage/index.htmlhttp://www.ibm.com/developerworks/vn/library/x-androidstorage/index.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/reference/android/texthttp://developer.android.com/reference/android/texthttp://developer.android.com/reference/android/texthttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/guide/topics/data/data-storage.htmlhttp://www.ibm.com/developerworks/vn/library/x-androidstorage/index.htmlhttp://www.ibm.com/developerworks/vn/library/x-androidstorage/index.htmlhttp://www.ibm.com/developerworks/vn/library/x-androidstorage/index.htmlhttp://www.ibm.com/developerworks/vn/library/x-androidstorage/index.htmlhttp://www.ibm.com/developerworks/vn/library/x-androidstorage/index.html
  • 7/31/2019 Accessing File

    18/18

    Thank you!