android--data persistence for internal storage, SDcard storage

Source: Internet
Author: User
Tags gettext sqlite sqlite database

Objective

I've been talking about Androidui, but it's not over yet, and I'll add it later. Today, I'll talk about other things about data persistence. For an application, it is unavoidable to be able to store the data, and the Android program is no exception. In Android, there are several ways to implement data persistence. The following will be described separately.

In Android, data persistence can be achieved in several ways:

    • Shared Preferences: A sharing Parameter form, a way to save data in the form of Key-value key-value pairs, Android built-in, general application configuration information, recommended for use in this way.
    • Internal Storage: Store data using the memory that comes with your Android device.
    • External Storage: The use of external storage devices to store data, generally referred to as sdcard.
    • SQLite Databases: Store structured data in a SQLite database.
    • Network Connection: Using a Web-based service to get data, see another blog: Android--apache HttpClient.

The following days will be introduced in the above several ways to achieve data persistence, for Sharedpreferences, previously wrote a blog, but he is not very satisfied, then there will be time to re-write a blog about sharedpreferences, Interested friends can go first to see, android--use Sharedpreferences. Let's introduce internal Storage and external Storage today.

Internal Storage

Internal storage, in Android, developers can directly use the device's internal memory to save the file, by default, the data is saved in this way can only be accessed by the current program, in other programs is inaccessible, and when the user uninstalls the program, these files will be deleted.

Using internal storage to save data, basically is to get a file output stream, and then write () the message to write to the output stream, and finally close the stream, which is the operation of the IO stream in Java. The steps are as follows:

    • Use the Context.openfileoutput () method to get to a FileOutputStream object.
    • Writes the content to be written to the FileOutputStream object through the Write () method.
    • Finally close the stream with close ().

The Context.openfileoutput () method described above has two overloaded functions, each of which has the following signatures:

    • FileOutputStream openfileoutput (String name): Opens the name file in Mode_private mode.
    • FileOutputStream openfileoutput (String name,int mode): Opens the name file in mode.

In the second overloaded function above, mode is the data of type int, this general use the constant parameter set in the context object, there are several:

    • Mode_append: Opens a file in an append way, and the content written using this mode is appended to the original content.
    • Mode_private: Private mode (default), if the file already exists, it will be recreated and replaced by the original file if it does not exist directly created.
    • Mode_world_readable: Opens the file in a read-only manner.
    • Mode_world_writeable: Opens the file in a write-only manner.

There are several ways to pay special attention, these methods for file relations to provide better support, in line with the way described above, you can do the file data for the regular CRUD operations (increase and deletion), the method is as follows:

    • File Getfilesdir (): Gets the absolute path of the file system.
    • Boolean DeleteFile (String name): Deletes a file that specifies the name of the file.
    • String[] FileList (): All filenames under the current app's internal storage path.

Speaking so much, here's a simple demo to illustrate what's mentioned above. In this demo, you can specify the file name and content to create files and to append, modify, delete, and query the contents.

Layout code:

 1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" match_parent "4 android:layout_height=" match_parent "5 android:orientation=" V Ertical "> 6 7 <textview 8 android:layout_width=" match_parent "9 android:layout_height=" Wrap_co         Ntent "android:text=" file name: "/>11 <edittext13 android:id=" @+id/etinternalfilename "14         Android:layout_width= "Match_parent" android:layout_height= "wrap_content"/>16-<textview18 Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "content:"         />21 <edittext23 android:id= "@+id/etinternalcontent" android:layout_width= "Match_parent" 25 android:layout_height= "Wrap_content"/>26 <linearlayout28 android:layout_width= "Match_parent" Android:layout_height= "Wrap_content" "android:orientation=" horizontal ">31 <button33 android:id=" @+id/             Btninternalsave "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "36             android:text= "Save"/>37 <button39 android:id= "@+id/btninternaldelete" 40 Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= E "/>43 <button45 android:id=" @+id/btninternalappend "android:layout_width= Wra" P_content "android:layout_height=" wrap_content "android:text=" append "/>49 [<b] Utton51 android:id= "@+id/btninternalquery" android:layout_width= "Wrap_content" roid:layout_height= "wrap_content" android:text= "Query"/>55 </linearlayout>56 <!--with a LISTVI EW to show all files under the current program's internal storage Path-->57 <listview58 android:id= "@+id/lvinternaldata" android:layout_width= "Match_parent" 60 android:layout_height= "Fill_parent" >61 </listview>62 </LinearLayout>

The

specifically writes an internal stored action class, and implements CRUD operations on it:

 1 package com.example.internal; 2 3 Import Java.io.ByteArrayOutputStream; 4 Import Java.io.File; 5 Import Java.io.FileInputStream; 6 Import java.io.FileNotFoundException; 7 Import Java.io.FileOutputStream; 8 Import java.io.IOException; 9 Import android.content.context;11 Import android.os.environment;12 import android.util.log;13 public class Myinte  Rnalstorage {15//Context16 private Context context;17 to save the current calling object Myinternalstorage (context context) {this.context = context;20}21/**22 * Save content to internal memory * @param filename filename * @param c Ontent content */26 public void Save (string filename, string content) throws IOException {//FILEOUTPUTST Ream Fos=context.openfileoutput (filename,28//context.mode_private); File File = new file (Context.getfi Lesdir (), filename), fileoutputstream fos = new FileOutputStream (file), Fos.write (Content.getbytes (   ); Fos.close (); 34  }35/**36 * Get content by filename * @param filename * * @return file Contents */40 public String get (Str ing filename) throws IOException {FileInputStream FIS = context.openfileinput (filename); Putstream BAOs = new Bytearrayoutputstream (); byte[] data = new byte[1024];44 int len = -1;45 wh Ile (len = fis.read (data))! =-1) {baos.write (data, 0, Len);}48 return new String (BAOs.      Tobytearray ()); 49}50/**51 * Add content to the end of the file in an append * * @param filename * * * @param content Append contents 54 */55 public void Append (string filename, string content) throws IOException {FileOutputStream Fos = con Text.openfileoutput (filename,57 context.mode_append); Fos.write (Content.getbytes ()); Os.close (); 60}61/**62 * Delete file * * @param filename * * * @return succeeded in */66 public bool EAN Delete (String FilenaMe) {context.deletefile return (filename); 68}69/**70 * Gets all filenames under the internal storage path. * @return File name Array 72 */73 public string[] Queryallfile () {return context.filelist (); 75}76 77}

Activity code:

  1 package Com.example.datastoragedemo;  2 3 Import java.io.IOException;  4 5 Import Com.example.internal.MyInternalStorage;  6 7 Import android.app.Activity;  8 Import Android.os.Bundle; 9 Import Android.view.View; Ten import Android.view.View.OnClickListener; Import Android.widget.AdapterView; Import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.ArrayAdapter; Import Android.widget.Button; Android.widget.EditText import; Android.widget.ListView import; + Import Android.widget.Toast; public class Internalstorageactivity extends Activity {private EditText etfilename, etcontent; priva Te Button btnsave, Btnquery, Btndelete, btnappend; -Private ListView Lvdata; @Override protected void OnCreate (Bundle savedinstancestate) {up//TODO auto-generated method Stub super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_internalstorage); Etfilename = (EditText) Findviewbyid (r.id.etinternalfilename); Etcontent = (EditText) Findviewbyid (r.id.etinternalcontent); Btnsave = (Button) Findviewbyid (R.id.btninternalsave); Btnquery = (Button) Findviewbyid (r.id.btninternalquery); Btndelete = (Button) Findviewbyid (r.id.btninternaldelete); Btnappend = (Button) Findviewbyid (r.id.btninternalappend); Lvdata = (ListView) Findviewbyid (r.id.lvinternaldata); PNS Btnsave.setonclicklistener (click); Btnquery.setonclicklistener (click); Btndelete.setonclicklistener (click); Btnappend.setonclicklistener (click); View.onclicklistener click = New Onclicklistener () {@Override-P              ublic void OnClick (View v) {myinternalstorage myinternal = null; String filename = null; 51 String content = null; (V.getid ()) {$ case R. id.btninternalsave:54 filename = Etfilename.gettext (). toString (); Content = Etcontent.gettext (). toString (); myinternal = new Myinternalstorage (internalstorageactivity.this); Myinternal.save (filename, content); Toast.maketext ( Internalstorageactivity.this, "Save file Succeeded", Toast.length_short). Show ();                     * catch (IOException e) {//TODO auto-generated Catch block 63 E.printstacktrace (); Toast.maketext (Internalstorageactivity.this, "Save file Failed", Toast.length_ Short). Show (); (); The case r.id.btninternaldelete:71 filename = Etfilename.gettext (). toString (); myinternal = new Myinternalstorage (internalstorageactivity.this);                73 Myinternal.delete (filename); Toast.maketext (internalstorageactivity.this, "Delete file succeeded", Toast.length_short). s how (); break; r.id.btninternalquery:78 myinternal = new Myinternalstorage (internalstorageactivity.t His); string[] files = Myinternal.queryallfile (); arrayadapter<string> Filearray = new Arrayadapter<string> (Bayi Inter Nalstorageactivity.this, and the android. R.layout.simple_list_item_1, files); Lvdata.setadapter (Filearray); Toast.maketext (internalstorageactivity.this, "Query file list", Toast.length_short). s how (); The break; The case r.id.btninternalappend:88 filename = Etfilename.gettext (). toString (); Content = Etcontent.gettext (). toString (); Myinternal = nEW Myinternalstorage (internalstorageactivity.this); try {myinternal.append (filename, content); Toast.maketex T (internalstorageactivity.this, "File content appended successfully", 94 Toast.length_short). Show ();                     (IOException e) {//TODO auto-generated catch block 97 E.printstacktrace (); 98 Toast.maketext (internalstorageactivity.this, "File content append failed", Toast.lengt H_short). Show (),}101 break;102}103 104}105};106 107 pri vate Onitemclicklistener ItemClick = new Onitemclicklistener () {108 109 @Override110 public void Onitemcli CK (adapterview<?> Parent, view view, int position,111 long ID) {LISTV ListView LV = Iew) parent;113 arrayadapter<string> adapter = (arrayadapter<string>) lv114. Getadapter (); String filename = Adapter.getitem (position); 116                     Etfilename.settext (filename); 117 Myinternalstorage myinternal = new Myinternalstorage (118 Internalstorageactivity.this); 119 String content;120 try {121 content = Myinternal                 . get (filename); 122 etcontent.settext (content); 123} catch (IOException e) {124  TODO auto-generated catch block125 e.printstacktrace (); 126}127 128}129};130 131}

Effect display, in the example, first add three files, and finally delete one, query the file list separately:

Using internal storage method for data persistence, the address of the file will be saved in the/data/data/<package_name>/files/path, the above created three files, and finally deleted one, if the use of the simulator, you can directly in the file Explorer View:

Caching (Cache)

Now that you've mentioned internal storage, here's a simple word about cache files. The operation of the cache files is basically the same as the files in the internal storage, except that the paths to the files are different. If you need to persist the data in a cached way, you need to use the Context.getcachedir () method to get the file save path.

For cache files, when the internal memory of the device is insufficient, Android will have the automatic deletion mechanism to delete these cache files, to restore the free space, so for the cache file, the content is generally best controlled under 1MB, and do not store important data, Because it is likely that the next time you go to fetch the data, it has been automatically cleaned up by the Android system.

External Storage

Using external storage for data persistence, where external storage is generally referred to as sdcard. Using sdcard stored data, without restricting access to the app, any app that can have access to SDcard permissions can be accessed, and SDcard is much larger than the internal storage space of the device, so generally larger data is stored in external storage.

Using SDcard to store data is basically the same way as internal storage, but there are three points to note:

    • 1th, it is necessary to first determine whether there is an available sdcard, which can be judged using a class environment that accesses the device environment variable, which provides a series of static methods to obtain the state of the current device, where there is a valid sdcard, Using the Environment.getexternalstoragestate () method, returns a string of data, environment encapsulates some final object to match, except Environment.media_ Mounted outside, the others are problematic, so only need to determine whether it is environment.media_mounted state.
    • 2nd, since the switch to SDcard, then the stored file path needs to be relatively changed, here you can use the Envir.getexternalstoragedirectory () method to get the root of the sdcard, you can access to the corresponding file.
    • 3rd, the need to give the application access to SDcard permissions, Android's rights control is particularly important, in the Android program, if you need to do some cross-border operation, you need to authorize it to access. Add code in Androidmanifest.xml: <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Because the way to access SDcard is similar to accessing internal storage, here is a save method for saving files and other CRUD operations, which are not given here.

1 public void Savetosdcard (string filename, string content) throws IOException {2  3         if (environment.media_mounte D.equals (Environment 4                 . Getexternalstoragestate ())) {5             log.i ("main", "This device has a memory card!") "); 6             File File = new file (Environment.getexternalstoragedirectory (), 7                     filename), 8             fileoutputstream fos = Null 9             fos = new FileOutputStream (file),             Fos.write (Content.getbytes ());             fos.close ();         }13     }

If you use SDcard to store files, the path is stored in the root directory of SDcard, if you use the emulator to run the program, the file created in the/mnt/sdcard/directory:

Add: For many Android devices now on the market, with a large storage space, generally 8GB or 16GB, and also support the sdcard extension, for such devices, using Enviroment.getexternalstoragedirectory ( ) method only gets the storage space that comes with the device, and for another extended sdcard, the path needs to be modified.

SOURCE download

Summarize

The above describes the methods of internal storage, external storage, cache storage. Developers can choose different ways to persist data as needed.

Please support the original, respect for the original, reproduced please indicate the source. Thank you.

android--data persistence for internal storage, SDcard storage

Related Article

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.