Android Buoi 11

3
Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com TRUNG TÂM ĐÀO TẠO MẠNG MÁY TÍNH NHẤT NGHỆ ĐỐI TÁC ĐÀO TẠO CỦA MICROSOFT TẠI VIỆT NAM 105 Bà Huyện Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com Buối 11: Data Storage – External Storage BT1: (External Storage) à Thẻ Nhớ Xây dựng ứng dụng Android đơn giản - Ghi 1 đoạn văn bản đã nhập vào file abc_in_sdcard.txt - Đọc lại file abc_in_sdcard.txt đã lưu Yêu cầu: Xây dựng phương thức kiemtra_thenho() dùng để kiểm tra tình trạng thẻ nhớ, trả về 0 hay 1 hay 2. Trong trường hợp thẻ nhớ cho phép, thực hiện các thao tác ghi, đọc file văn bản đơn giản. Khai báo cấp phép ghi vào thẻ nhớ trong tập tin AndroidManifest.xml Kiểm tra thẻ nhớ của máy giả lập Simulator thực sự có file abc_in_sdcard chưa?

description

Bài tập android buổi 11

Transcript of Android Buoi 11

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    Bui 11: Data Storage External Storage BT1: (External Storage) Th Nh

    Xy dng ng dng Android n gin

    - Ghi 1 on vn bn nhp vo file abc_in_sdcard.txt - c li file abc_in_sdcard.txt lu

    Yu cu:

    Xy dng phng thc kiemtra_thenho() dng kim tra tnh trng th nh, tr v 0 hay 1 hay 2.

    Trong trng hp th nh cho php, thc hin cc thao tc ghi, c file vn bn n gin.

    Khai bo cp php ghi vo th nh trong tp tin AndroidManifest.xml

    Kim tra th nh ca my gi lp Simulator thc s c file abc_in_sdcard cha?

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    public class Demo_externalActivity extends Activity { EditText et1; Button b1,b2,b3; public int kiemtra_thenho(){ // 2: read and write; 1: read only ; 0: cannot read/write int temp=0; String state=Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) temp=2; else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) temp=1; return temp; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1=(EditText) findViewById(R.id.editText1); b1=(Button) findViewById(R.id.button1); b2=(Button) findViewById(R.id.button2); b3=(Button) findViewById(R.id.button3); b1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { et1.setText(""); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { try{ File myFile = new File("/sdcard/abc_in_sdcard.txt"); myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); String chuoi=et1.getText().toString(); fOut.write(chuoi.getBytes()); fOut.close(); Toast.makeText(getBaseContext(), "Thnh cng ghi tp tin 'abc_in_sdcard.txt'", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } } }); b3.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { try { File myFile = new File("/sdcard/abc_in_sdcard.txt");

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    FileInputStream fIn = new FileInputStream(myFile); int size=fIn.available(); byte[] buffer=new byte[size]; fIn.read(buffer); fIn.close(); String chuoi=new String(buffer); et1.setText(chuoi); Toast.makeText(getBaseContext(), "Thnh cng c tp tin 'abc_in_sdcard.txt'", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } } }); }

    }

    BT2 (External Storage):

    Dng DDMS (Dalvik Debug Monitoring Service) chp 1 s file hnh nh, mp3, video vo th nh ca my gi lp.

    Xy dng ln lt cc ng dng Android n gin

    - Hin th cc hnh nh t th nh - Play 1 file mp3 t th nh - Play 1 file video t th nh.