File, Custom Formating

Download Report

Transcript File, Custom Formating

Acessing file
•
•
•
•
Cource:
Instructor:
Student :
Email:
Embedded Systems and Realtime
Dr. Tran Thi Minh Chau
Nguyen Van Lan
[email protected]
Out line:
•
Data Storage & Accessing File
Shared Preferences
Internal Storage
External Storage
SQLite Databases
•
Custom Format Text ( My Project)
HTML & Spanned
Demo
Data Storage
• Trong android, Data được lưu trữ với nhiều option khác nhau:
Shared Preferences
Dữ liệu là private, dưới dạng cặp Key-Value
Internal Storage
Lưu dữ liệu ở chế độ private và lưu trên các thiết bị vật lý
External Storage
Lưu dữ liệu ở chế độ public và lưu trên các thiết bị vật lý
ngoài như Sdcard
SQLite Databases
Là hình thức lưu dữ liệu có cấu trúc và hỗ trợ truy vấn
theo câu lệnh SQL
Network Storage
JDBC
Shared Preferences
Là hình thức lưu trữ dạng <Name, Value>
Dữ liệu là kiểu cơ bản: booleans, floats, ints, longs, and strings.
Hiệu quả trong việc lưu thông tin cấu hình, thông tin kết nối,
ứng dụng….
Có gì đó giống với Bundles
Bền vững hơn Bundles
Shared Preference API
•
•
•
•
•
•
•
Tạo một đối tượng Shared Preferences
getShared Preferences()
getPreferences()
Để thao tác:
Gọi edit() trả về đối tượng SharedPreferences.Editor
Thêm giá trị : putBoolean(),putString()
Commit() để thực hiện thao tác
• Tham khảo thêm
:http://developer.android.com/reference/android/content/Sh
aredPreferences.html
Internal Storage
• File được lưu trực tiếp trên thiết bị bộ nhớ trong.(Internal
Storage)
• Dữ diệu này là private và chỉ chịu sự quản lí của chính ứng
dụng đang chạy nó, các ứng dụng khác không thể truy cập
được
• Tạo ghi private file trong Internal Storage:
• Dùng openFileOutput() phương thức này trả về đối tượng
FileOutputStream:
• Code : FileOutputStream f = openFileOutput( FILENAME,
Context.MODE_PRIVATE);
f.write(string.getBytes()); // ghi file
f.close(); // đóng tream lại
• Khi đọc file từ bộ nhớ trong ta dùng phương thức
openFileInput() để tạo một đối tượng FileInputStream
• Sau đó thao tác với file bằng các phương thức: read(),close()
FileInputStream.read()
Tip : Những file thường xuyên sử dụng trong quá trình chạy ứng
dụng ta cần lưu chúng cố định tại một nơi : res/raw
OpenRawResource()
-> Lưu cache files:
Tăng tốc độ cho chương trình, tuy nhiên phải thường xuyên dọn
dẹp file cache để tối ưu không gian nhớ trong
External Storage
• Lưu trữ ngoài ( External Storage )
Ưu điểm:
Không gian lưu trữ lớn
Dễ di chuyển giữa các thiết bị :PC, Mobile
Dễ thay thế nâng cấp
Nhược điểm:
Tính bảo mật không cao : public data
Tốc độ truy cập thấp
Accessing file on External Storage
• Trước khi dùng External Storages cần phải kiểm tra trạng trái thiết bị
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;
}
Accessing
• Mở một file: getExternalFilesDir() (API >= 8)
getExternalStorageDirectory() (API < 7)
Thư mực để viết dữ liệu:
/Android/data/<package_name>/files/
<package_name>: tên package định dạng theo kiểu trong
java
Lưu ý : cần phải khai báo quyền truy cập trong manifest
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORA
GE"/>
Thao khảo : http://www.youtube.com/watch?v=_WtTmPmrzI&feature=relmfu
Custom Fomat text
Spanned & HTML
CharSequence
•
Int getSpanStart(Object)
Int getSpanEnd(Object)
T[] getSpans(int start,int end,class<T>)
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()
Spanned
SpannedString
•
•
Spannable
Immutable markup, immutable text
Mutable markup, immutable text
SpannableString
Immutable markup, immutable text
Editable
Interface
SpannableStringBuilder
Implementation
Presentation and saving
Presentation
Saving
Spanned Html.fromHtml(String)
String Html.toHtml(Spanned)
Đổi một đoạn mã HTML sang một
đối tượng Spanned
Biểu diễn đối tượng Spanned
bằng mã HTML
Edittext.setText(Spanned)
Biểu diễn Spanned ra bằng một
TextView
Đến đây ta có thể lưu đối tượng
vào CSDL và lần sau gọi lại
Người dùng thao tác
với Edit text
Saving
Html.toHtml
Convert to Html
Spanned
String
Database
Convert to Spanned
String
Html.fromHtml
Presentation
Spanned
But …..
• Không phải thẻ HTML nào cũng hỗ trợ bởi phương thức Html.fromHtml,
Html.toHtml
• Ta phải Implement interface Html.TagHandler
Public Class myHandler implements TagHandler{
public void handleTag(boolean opening,String tag, Editable
output,XMLreader xmlReader){
// doing somethings with tag
}
}
Khi sử dụng Spanned Html.fromHtml(String source, ImageGetter null,
myHandler handler);
Ở phần này ta tham khảo ở:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.a
ndroid/android/2.2_r1.1/android/text/Html.java
Conclusion
• Phần đầu tiên Accessing File là kỹ thuật cơ bản trong phát
triển ứng dụng Android
• Trong phần Custom format text là giải pháp cá nhân em đưa ra
trong quá trình làm Project cho ứng dụng Nhật ký
Reference:
File :
Android-Chapter15-Files.pptx [Cleverland University]
http://www.ibm.com/developerworks/vn/library/xandroidstorage/index.html
http://developer.android.com/guide/topics/data/datastorage.html
http://developer.android.com/reference/android/text
Thank you!