Android data storage method and storage location

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.