Android data storage method and storage location
Android data storage is an important step. There are several methods:
1. Shared Preference
Usage:
SharedPreferences sp = MainHello.this.getSharedPreferences("hello", 0);int age = sp.getInt(“Age”, 0);// readSharedPreferences.Editor ed = sp.edit();// writeed.putString("Name", "mike");ed.putInt("Age", 33);ed.putFloat("Salary", 1080.55f); ed.commit();
Storage location:
Data is stored in xml format, located
/Data/ /Shared_prefs/hello. xml
XML file Content:
mike
2. Internal Storage
Usage:
try {String str = "Hello World";FileOutputStream fos = MainHello.this.openFileOutput("hello.txt", MODE_PRIVATE);fos.write(str.getBytes());fos.close();} catch (Exception e) {e.printStackTrace();}
Storage location:
/Data/ /Files/hello.txt
Iii. External Storage
Usage:
String str = "Hello World";File file = new File(getExternalFilesDir(null), "hello.txt");FileOutputStream fos = new FileOutputStream(file);fos.write(str.getBytes());fos.close();
Storage location:
/Mnt/sdcard/Android/data/ /Files/hello.txt
Iv. SQL Lite
Usage:
Public class MyDB extends SQLiteOpenHelper {public MyDB (Context context) {super (context, "TestDB", null, 1) ;}@ Overridepublic void onCreate (SQLiteDatabase db) {String SQL = "create table stu (" + "sid INTEGER," + "sname TEXT)" mongodb.exe cSQL (SQL) ;}@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// Used when mode update} MyDB d = new MyDB (MainHello. this); SQLiteDatabase db = d.getwritabledatabase(mongomongodb.exe cSQL ("insert into stu (sid, sname) VALUES (3, 'jason ')");
Storage location:
/Data/com. hezongjian/databases/
5. Content Provider
Usage:
ContentResolver cr = getContentResolver();Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, null); cur.moveToFirst(); int num = cur.getColumnIndex(CallLog.Calls.NUMBER); do{ Log.e("CALLLOG", cur.getString(num));}while(cur.moveToNext()); cur.close();// android.permission.READ_CONTACTS permission