Thứ Bảy, 29 tháng 6, 2013

Lưu trữ internal sd(vùng nhớ trong) và external sd(vùng nhớ ngoài)

Hôm nay vọc về vấn đề lưu file trong bộ nhớ trong và bộ nhớ ngoài trong android ,test được ví dụ thấy hay hay : Đây là giao diện:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:text="Android Storage" />

    <EditText
        android:id="@+id/inputstore"
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save Internal" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Internal" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save External" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get External" />

    <TextView
        android:id="@+id/outputstore"
        android:layout_width="match_parent"
        android:layout_height="92dp"
        android:ems="10" />

</LinearLayout>

      
Sau đó mình tạo một Activity để lưu trữ và đọc file từ bộ nhớ trong và ngoài:

package example.storage;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.example.test.R;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidStorageActivity extends Activity implements OnClickListener{
private final String filename = "bom.txt";
private final String dirname = "MyFolderBom";
File myInternal;
File myExternal;
File directory ;
  @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.example_storage_android_storage);
ContextWrapper context = new ContextWrapper(getApplicationContext());
directory = context.getDir(dirname,Context.MODE_PRIVATE);
myInternal = new File(directory,filename);

Button saveInternal = (Button)findViewById(R.id.button1);
saveInternal.setOnClickListener(this);

Button getInternal = (Button)findViewById(R.id.button2);
getInternal.setOnClickListener((android.view.View.OnClickListener) this);

Button saveExternal = (Button)findViewById(R.id.button3);
saveExternal.setOnClickListener((android.view.View.OnClickListener) this);

Button getExternal = (Button)findViewById(R.id.button4);
getExternal.setOnClickListener((android.view.View.OnClickListener) this);

}
  

public void onClick(View v) {
// TODO Auto-generated method stub
EditText inputtext = (EditText)findViewById(R.id.inputstore);
TextView outputtext = (TextView)findViewById(R.id.outputstore);
String myData="";
myExternal = new File(getExternalFilesDir(dirname),filename);
switch(v.getId())
{
case R.id.button1:
 try {
 FileOutputStream fos = new FileOutputStream(myInternal);
 fos.write(inputtext.getText().toString().getBytes());
 fos.close();
 } catch(IOException e){
 e.printStackTrace();
 }
inputtext.setText("");
outputtext.setText("MyFolder is saved internal"+directory);break;
case R.id.button2:
try{ 
FileInputStream fis = new FileInputStream(myInternal);
DataInputStream fi = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(fi)); 
  
String strLine ; 
while((strLine = br.readLine())!=null){
myData = myData + strLine;
}
    fi.close();
}catch(IOException e){
e.printStackTrace();
}
inputtext.setText(myData);
outputtext.setText("Data duoc lay tu internal storage");break;
case R.id.button3:
 
try{
FileOutputStream fo = new FileOutputStream(myExternal);
fo.write(inputtext.getText().toString().getBytes());
}catch (IOException e)
{
e.printStackTrace();
}
    inputtext.setText("");
    outputtext.setText("bom.txt save to External "+getExternalFilesDir(dirname));
    break;
 
case R.id.button4:
 try {
   FileInputStream fis = new FileInputStream(myExternal);
   DataInputStream in = new DataInputStream(fis);
   BufferedReader br = 
    new BufferedReader(new InputStreamReader(in));
   String strLine;
   while ((strLine = br.readLine()) != null) {
    myData = myData + strLine;
   }
   in.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  inputtext.setText(myData);
  outputtext
  .setText("bom.txt data retrieved from Internal Storage...");
  break;
 
}
}


}

 Cái này thì cũng đơn giản ,quan trọng nhất là các dòng:
  * ContextWrapper context = new ContextWrapper(getApplicationContext());
directory = context.getDir(dirname,Context.MODE_PRIVATE);

ContextWrapper dùng để lấy phạm vi trong application đang chạy của bạn,từ đó mình dùng getDir() để lấy đường dẫn đến bộ nhớ trong,đồng thời tạo luôn thư mục và đường dẫn từ dirname.

  * myExternal = new File(getExternalFilesDir(dirname),filename);

    Mình dùng hàm getExternalFilesDir() để lấy đường dẫn đến bộ nhớ ngoài.

Các hàm còn lại chủ yếu là đọc ,xuất file nguồn như trong java thôi.

Không có nhận xét nào:

Đăng nhận xét