1. SharedPreference ▪ Khái niệm lưu trữ ▪ Khai báo và sử dụng

2. Bộ nhớ thiết bị

 Android cung cấp một số lựa chọn để lưu trữ dữ

liệu

 Lựa chọn giải pháp nào tùy thuộc vào nhu cầu bảo mật của ứng dụng và kích thước của file cần lưu trữ:

 Các tùy chọn lưu trữ bao gồm:

 Shared Preferences  Internal Storage  External Storage  SQLite Database  Network Connection

3

4

 SharedPreference cho phép thực hiện lưu trữ và truy xuất dữ liệu theo cặp khóa key-value cho các kiểu dữ liệu cơ bản.

 Có thể lưu trữ các kiểu dữ liệu cơ bản sau:

 Boolean  Float  Int  Long  String

 Dữ liệu vẫn được bảo toàn ngay cả khi ứng dụng bị

đóng hoàn toàn.

 Khai báo truy xuất:

 Có thể sử dụng một trong 2 phương thức sau

▪ getSharedPreferences: sử dụng truy xuất đến tập tin Preferences đã lưu trữ bằng cách truyền vào tên tập tin.

▪ getPreferences: truy xuất đến tập tin Preference

dẫn data/data//shared_prefs/.xml

mặc định tương ứng với Activity gọi thực thi.  Mặc định tập tin Preference được lưu trữ theo đường

 Khai báo truy xuất:

 Các định dạng mở tập tin khi truy xuất Preference:

▪ MODE_PRIVATE ▪ MODE_APPEND ▪ MODE_WORLD_READABLE (Deprecated in API 17) ▪ MODE_WORLD_WRITEABLE (Deprecated in API 17) ▪ MODE_MULTI_PROCESS

 Tạo đối tượng SharedPreferences:

 Lưu:

SharedPreferences sp = getSharedPreferences("file_name",MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit();

editor.putAAA(key, value)

 Đọc: datatype variable = sp.getAAA(key, default_value)

Trong đó AAA có thể là Boolean, Float, Int, Long, String

 Lưu trữ dữ liệu:

 Tạo đối tượng Editor từ phương thứ edit() của Preference  Lưu trữ dữ liệu vào Preference thông qua phương thức

put: ▪ putBoolean(Key, Value) ▪ putFloat(Key, Value) ▪ putInt(Key, Value) ▪ putLong(Key, Value) ▪ putString(Key, Value)

 Ví dụ:

boolean wifi_status = checkBoxWifi.isChecked(); SharedPreference shared_pref = getPreferences(MODE_PRIVATE); Editor editor = shared_pref.edit(); editor.putBoolean(“Wifi_Status”, wifi_status); editor.apply();

 Khai báo truy xuất:

 Các giá trị lưu trữ được truy xuất thông qua phương thức

get: ▪ getBoolean(Key, DefValue) ▪ getFloat(Key, DefValue) ▪ getInt(Key, DefValue) ▪ getLong(Key, DefValue) ▪ getString(Key, DefValue)

 Ví dụ:

boolean wifi_status =

getPreferences(MODE_PRIVATE).getBoolean(“Wifi_Status”);

checkBoxWifi.setChecked(wifi_status);

private void saveSharedPreferences() { //Đưa dữ liệu vào SharedPreferences editor.putString("username",

txtUsername.getText().toString());

editor.putString("password",

txtPassword.getText().toString());

editor.putBoolean("remember",

chkRemember.isChecked());

//Hoàn tất editor.commit();

}

11

private void loadSharedPreferences() {

boolean isRemember =

sp.getBoolean("remember", false);

txtUsername.setText(

sp.getString("username", ""));

txtPassword.setText(

sp.getString("password", ""));

}

12

13

 Bộ nhớ trong được cấp phát khi ứng dụng được cài đặt

 Chỉ có ứng dụng mới có thể truy xuất được bộ nhớ của

trên thiết bị trên một thư mục riêng biệt:

 Phương thức truy xuất:

ứng dụng đó.  Đường dẫn các tập tin được lưu trữ: data/data//files

getFilesDir - File

 Truy xuất ghi tập tin:

 Gọi phương thức openFileOutput() nhận về dữ liệu

 Ví dụ:

stream cho đối tượng FileOutStream.  Đọc dữ liệu với phương thức write().  Đóng stream bằng phương thức close().

String hello = “Hello Android!”; FileOutStream fos = openFileOutput(“Hello_file.txt”,

MODE_APPEND);

fos.write(hello.getBytes()); fos.close();

 Truy xuất đọc tập tin:

 Gọi phương thức openFileInput() nhận về dữ liệu

 Đọc dữ liệu với phương thức read().  Đóng stream bằng phương thức close().

 Ví dụ:

stream cho đối tượng FileInputStream.

s += (char)b;

FileInputStream fis = openFileInput(“Hello_file.txt”); int b = 0; String s = “”; while ((b = fis.read()) != 0)

fis.close(); Log.d(“Noi dung xuat”, s);

 Mỗi thiết bị Android cung cấp bộ nhớ ngoài cho phép

 Bộ lưu trữ ngoài bao gồm hai dạng:  Bộ nhớ thiết bị (non-removable)  Bộ nhớ ngoài (sd-card, usb…)

 Cần xin cấp quyền để truy xuất bộ nhớ này

android.permission.WRITE_EXTERNAL_STORAGE

người dùng có thể lưu trữ và truy xuất trực tiếp trên thiết bị hoặc thông qua máy tính khi kết nối USB Storage.

 Ứng dụng được cấp phát bộ nhớ ngoài để lưu trữ dữ liệu, mặc định không thể truy xuất như tập tin Media.  Địa chỉ lưu trữ: /mnt/sdcard/Android//files  Phương thức truy xuất:

▪ getExternalFilesDir(String) – File ▪ getExternalFilesDirs(String) – File[] (API 19 - KITKAT) ▪ ContextCompat.getExternalFilesDirs(String) – File[] (Support

Library)

 Cấu hình cài đặt ứng dụng trên bộ nhớ trong hoặc ngoài thông qua cặp thẻ trong AndroidManifest.  installLocation ▪ internalOnly ▪ Auto ▪ preferExternal

 Kiểm tra tình trạng bộ nhớ:

 Cần kiểm tra tình trạng bộ nhớ ngoài trước khi thực hiện các thao tác.  Truy xuất trạng thái bộ nhớ ngoài thông qua phương thức getExternalStorageState trong đối tượng Environment.

 Các thông số được trả về bao gồm:

▪ MEDIA_UNKNOWN ▪ MEDIA_REMOVED ▪ MEDIA_UNMOUNTED ▪ MEDIA_CHECKING ▪ MEDIA_NOFS ▪ MEDIA_MOUNTED ▪ MEDIA_MOUNTED_READ_ONLY ▪ MEDIA_SHARED ▪ MEDIA_BAD_REMOVAL ▪ MEDIA_UNMOUNTABLE

 Kiểm tra tình trạng bộ nhớ:

 Ví dụ kiểm tra có thể đọc ghi trên bộ nhớ ngoài public boolean isExternalStorageWritable() {

String state = Environment.getExternalStorageState(); if( Environment.MEDIA_MOUNTED.equals(state))

}

return true; return false;

 Một số phương thức hỗ trợ duyệt tập tin trong

bộ nhớ:  isFile() – boolean  isDirectory() – boolean  getAbsolutePath() - String  getPath() - String  list() – String[]  mkdir() - boolean  mkdirs() – boolean  delete() - boolean

 Một số thư mục dùng chung:

 Thư mục:

▪ DIRECTORY_ALARMS ▪ DIRECTORY_DCIM ▪ DIRECTORY_DOCUMENTS (API 19) ▪ DIRECTORY_DOWNLOADS ▪ DIRECTORY_MOVIES ▪ DIRECTORY_MUSIC ▪ DIRECTORY_NOTIFICATIONS ▪ DIRECTORY_PICTURES ▪ DIRECTORY_POSTCASTS ▪ DIRECTORY_RINGTONES

23

24

 Slide bài giảng Lập trình Android, T3H  http://developer.android.com/intl/vi/training/basic

s/data-storage/index.html

25

26