Four ways to develop data storage for Android

Source: Internet
Author: User

Four ways to develop data storage for Android

in Xiamen to do Android development also has two months, fast Valentine's Day, I still get the code. Develop your own app on the platform, use the knowledge of data storage, and now summarize:

in general, there are three ways to store data: One is a file, one is a database, the other is a network. where the file and database may use a little more, the file is more convenient to use, the program can define its own format; database with a little annoying lock some, but it has its advantages, such as the high performance in the massive data, have query function, can be encrypted, can be locked, can be cross-application, cross-platform and so on; is used to compare important things, such as scientific research, exploration, aviation and other real-time collected data need to be transferred over the network to the data processing center for storage and processing.

for the Android platform, it is stored in a way that is also the case, according to the overall score, but also files, databases and networks. But from the developer's point of view it can be divided into the following five ways:
1.SharedPreferences Sharing Preferences
2.Internal Storage internal storage space
3.External Storage External storage space
4.SQLite Database
5.Internet Network
Each of these ways has its own advantages and disadvantages, according to different actual conditions to choose, and can not give a unified standard. Here are a few ways to talk about their pros and cons, and the most appropriate use cases:

Below according to oneself in the development, do a test separately, first do a simple app, help the group to understand the Android data storage:

(i) Documents

In fact, this app is also downloaded on the Internet, the effect


function: Download a picture of the network to your own collection.

now put the code on it: 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 Copyofmainactivity extends Activity implements Runnable {
Private EditText Urltext;
Private button button;
Private Handler Handler; Declares 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 (); Open Thread
Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
if (flag) {
Toast.maketext (copyofmainactivity.this, "File download 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 (); Get 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 Message
}
}

2.main.xml Code

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal"
android:background= "@drawable/background"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<edittext
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"/>
<button
Android:id= "@+id/button_go"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/go"/>

</LinearLayout>

3.string.xml Code

<?xml version=" 1.0 "encoding=" Utf-8 "?
<resources


    <string name= "Hello" >hello world, Mainactivity!</string>
    <string name= "App_name" >14.5</STRING>
<string name= "Go" > Download </string>
<string name= "DefaultValue" >http:// P8.qhimg.com/dm/620_270_/t011685779b50979038.jpg</string>
</resources> 

run to get the diagram above.

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.