關於Android開發資料存放區的方式(一)

來源:互聯網
上載者:User

關於Android開發資料存放區的方式(一)

 

關於Android開發資料存放區方式(一)

 

在廈門做Android開發也有兩個月了,快情人節了,我還在弄代碼。在平台上開發自己的APP,用到了資料存放區的知識,現在總結一下:

總體的來講,資料存放區方式有三種:一個是檔案,一個是資料庫,另一個則是網路。其中檔案和資料庫可能用的稍多一些,檔案用起來較為方便,程式可以自己定義格式;資料庫用起稍煩鎖一些,但它有它的優點,比如在海量資料時效能優越,有查詢功能,可以加密,可以加鎖,可以跨應用,跨平台等等;網路,則用於比較重要的事情,比如科研,勘探,航空等即時採集到的資料需要馬上通過網路傳輸到資料處理中心進行儲存並進行處理。

對於Android平台來講,它的儲存方式也不外乎這幾種,按方式總體來分,也是檔案,資料庫和網路。但從開發人員的角度來講它可以分為以下五種方式:
1.SharedPreferences共用偏好
2.Internal Storage內部儲存空間
3.External Storage外部儲存空間
4.SQLite Database資料庫
5.Internet網路
這幾種方式各自有各自的優點和缺點,要根據不同的實際情況來選擇,而無法給出統一的標準。下面就各種方式談談它們的優缺點,以及最合適的使用方式:

下面根據自己在開發中,分別做一下測試,先做一下簡單的APP,幫組自己理解Android的資料存放區:

(一)檔案

其實這個APP 也是在網上下載的,效果

 

實現的功能:將網路上的一張圖片下載到自己的收集裡面。

現在就貼一下代碼吧: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; // 聲明一個Handler對象
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(); // 開啟線程
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (flag) {
Toast.makeText(CopyOfMainActivity.this, 檔案下載完成!,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CopyOfMainActivity.this, 檔案下載失敗!,
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(); // 擷取輸入資料流對象
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(); // 關閉輸入資料流對象
urlConn.disconnect();
flag = true;
} catch (MalformedURLException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
Message m = handler.obtainMessage(); // 擷取一個Message
handler.sendMessage(m); // 發送訊息
}
}

2.main.xml代碼


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代碼





Hello World, MainActivity!
14.5
下載
http://p8.qhimg.com/dm/620_270_/t011685779b50979038.jpg
 

運行就可以得到如上的圖。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.