Data storage for Android development (1)
Data Storage Method for Android development (1)
I have been developing Android in Xiamen for two months. It's almost valentine's day, and I am still working on code. Develop your own APP on the platform and use the knowledge of data storage. Here we will summarize:
Generally, there are three data storage methods:One is a file, the other is a database, and the other is a network.Among them, files and databases may be used a little more, files are more convenient to use, and programs can define their own formats; databases may be slightly difficult to use, but it has its advantages, for example, in the case of massive data volumes, the performance is superior, with query functions, encryption, locks, cross-application, and cross-platform functions. networks are used for important tasks, such as scientific research and exploration, real-time data collected by airlines needs to be immediately transmitted to the data processing center through the network for storage and processing.
For the Android platform, its storage methods are similar to these. They are classified as files, databases, and networks in general. But from the developer's point of view, it can be divided into the following five methods:
1. SharedPreferences sharing preferences
2. Internal Storage space
3. External Storage
4. SQLite Database
5. Internet Network
These methods have their own advantages and disadvantages. They should be selected based on different actual conditions, but they cannot provide unified standards. The following describes their advantages and disadvantages and the most appropriate use cases in various ways:
The following is a test based on your own development. A simple APP is used to help the Group understand Android Data Storage:
(1) files
In fact, this APP is also downloaded online.
Function: download an image from the network to your collection.
Paste the code: 1. MainActivity. java
Package com. test. learning;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java.net. HttpURLConnection;
Import java.net. MalformedURLException;
Import java.net. URL;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity implements Runnable {
Private EditText urlText;
Private Button button;
Private Handler handler; // declare a Handler object
Private boolean flag = false;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
UrlText = (EditText) findViewById (R. id. editText_url );
Button = (Button) findViewById (R. id. button_go );
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Thread t = new Thread (CopyOfMainActivity. this );
T. start (); // enable the thread
Handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
If (flag ){
Toast. makeText (CopyOfMainActivity. this, the file download is complete !,
Toast. LENGTH_SHORT). show ();
} Else {
Toast. makeText (CopyOfMainActivity. this, File Download failed !,
Toast. LENGTH_SHORT). show ();
}
Super. handleMessage (msg );
}
};
}
});
}
@ Override
Public void run (){
Try {
String sourceUrl = urlText. getText (). toString ();
URL url = new URL (sourceUrl );
HttpURLConnection urlConn = (HttpURLConnection) url
. OpenConnection ();
InputStream is = urlConn. getInputStream (); // gets the input stream object
If (is! = Null ){
String expandName = sourceUrl. substring (
SourceUrl. lastIndexOf (.) + 1, sourceUrl. length ())
. ToLowerCase ();
String fileName = sourceUrl. substring (
SourceUrl. lastIndexOf (/) + 1,
SourceUrl. lastIndexOf (.));
File file = new File (/sdcard/pictures/+ fileName +.
+ ExpandName );
FileOutputStream fos = new FileOutputStream (file );
Byte buf [] = new byte [128];
While (true ){
Int numread = is. read (buf );
If (numread <= 0 ){
Break;
} Else {
Fos. write (buf, 0, numread );
}
}
}
Is. close (); // close the input stream object
UrlConn. disconnect ();
Flag = true;
} Catch (MalformedURLException e ){
E. printStackTrace ();
Flag = false;
} Catch (IOException e ){
E. printStackTrace ();
Flag = false;
}
Message m = handler. obtainMessage (); // get a Message
Handler. sendMessage (m); // send a message
}
}
2. main. xml Code
Android: orientation = horizontal
Android: background = @ drawable/background
Android: layout_width = fill_parent
Android: layout_height = fill_parent>
Android: layout_weight = 1
Android: id = @ + id/editText_url
Android: layout_height = wrap_content
Android: layout_width = 0dp
Android: text = @ string/defaultvalue
Android: lines = 1/>
Android: id = @ + id/button_go
Android: layout_width = wrap_content
Android: layout_height = wrap_content
Android: text = @ string/go/>
3. string. xml Code
Hello World, MainActivity!
14.5
Download
Http://p8.qhimg.com/dm/620_270_/t011685779b50979038.jpg
Run the command to obtain the figure above.