Selamat malam, apa kabar semuanya, semoga anda senantiasa berada dalam lindungan Tuhan. Seperti biasanya saya akan membagikan postingan tentang tutorial membuat penyimpanan data ke memory external. Pada metoda ini, data disimpan langsung ke sebuah file yang kita beri nama akan tersimpan di SD Card atau Memory external.

Untuk lebih lengkapnya kita ikuti saja tutorial dibawah ini.
Pada folder res/layout ubah file activity_main.xml menjadi seperti dibawah ini.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = ".MainActivity" > < TextView android:id = "@+id/textView6" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView3" android:layout_below = "@+id/editText3" android:layout_marginTop = "14dp" android:text = "NAMA FILE" /> < Button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView6" android:layout_below = "@+id/editText4" android:layout_marginTop = "14dp" android:text = "Simpan Ext" /> < EditText android:id = "@+id/editText1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/editText2" android:layout_alignParentTop = "true" android:layout_alignRight = "@+id/editText2" android:ems = "10" /> < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/editText1" android:layout_alignBottom = "@+id/editText1" android:layout_alignParentLeft = "true" android:text = "NPM" /> < TextView android:id = "@+id/textView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView1" android:layout_below = "@+id/editText1" android:layout_marginTop = "14dp" android:text = "NAMA" /> < EditText android:id = "@+id/editText2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/textView2" android:layout_alignBottom = "@+id/textView2" android:layout_alignLeft = "@+id/editText3" android:layout_alignRight = "@+id/editText3" android:ems = "10" /> < EditText android:id = "@+id/editText3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/textView3" android:layout_alignBottom = "@+id/textView3" android:layout_toRightOf = "@+id/button1" android:ems = "10" /> < TextView android:id = "@+id/textView3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView2" android:layout_below = "@+id/editText2" android:layout_marginTop = "28dp" android:text = "ALAMAT" /> < EditText android:id = "@+id/editText4" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/textView6" android:layout_alignBottom = "@+id/textView6" android:layout_alignLeft = "@+id/editText3" android:layout_alignRight = "@+id/editText3" android:ems = "10" > < requestFocus /> </ EditText > < Button android:id = "@+id/button3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/button1" android:layout_alignBottom = "@+id/button1" android:layout_alignLeft = "@+id/editText4" android:text = "Baca Ext" /> </ RelativeLayout > |
Selanjutnya, ubah file MainActivity.java seperti koding dibawah ini.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package com.android.external; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.view.View.OnClickListener; public class MainActivity extends Activity { Button simpeks, bacaeks; EditText nim, nama, alamat, namafile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nim = (EditText) findViewById(R.id.editText1); nama = (EditText) findViewById(R.id.editText2); alamat = (EditText) findViewById(R.id.editText3); namafile = (EditText) findViewById(R.id.editText4); simpeks = (Button) findViewById(R.id.button1); bacaeks = (Button) findViewById(R.id.button3); simpeks.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String filename = namafile.getText().toString(); String nimm = nim.getText().toString(); String namaa = nama.getText().toString(); String alamatt = alamat.getText().toString(); FileOutputStream fos; try { File filesaya = new File("/sdcard/" + filename); filesaya.createNewFile(); FileOutputStream fout = new FileOutputStream(filesaya); OutputStreamWriter myoutwriter = new OutputStreamWriter( fout); myoutwriter.append(".:: BIODATA ::.\n"); myoutwriter.append("\nNIM : " + nimm + "\n"); myoutwriter.append("\nNAMA : " + namaa + "\n"); myoutwriter.append("\nALAMAT : " + alamatt + "\n"); myoutwriter.close(); fout.close(); Toast.makeText(getApplicationContext(), filename + " tersimpan", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); bacaeks.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String filename = namafile.getText().toString(); StringBuffer stringbuper = new StringBuffer(); String adatarow = ""; String abuper = ""; try { File filesaya = new File("/sdcard/" + filename); FileInputStream FiS = new FileInputStream(filesaya); BufferedReader mYreader = new BufferedReader( new InputStreamReader(FiS)); while ((adatarow = mYreader.readLine()) != null) { abuper += adatarow + "\n"; } mYreader.close(); } catch (IOException e) { e.printStackTrace(); } // TODO: handle exception Toast.makeText(getApplicationContext(), abuper, Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } |
Jalankan project anda, masukkan data di form sesuai label. Lalu tekan tombol simpan.

Jika sudah tersimpan, untuk melihat data tersebut tekan tombol Baca. Selamat mencoba.

No comments:
Post a Comment