External Memory in Android

FilesActivity.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 |
package com.example.files; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class FilesActivity extends Activity { EditText fname,fcontent,fnameread; Button write,read; TextView filecon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_files); fname = (EditText)findViewById(R.id.fname); fcontent = (EditText)findViewById(R.id.ftext); fnameread = (EditText)findViewById(R.id.fnameread); write = (Button)findViewById(R.id.btnwrite); read = (Button)findViewById(R.id.btnread); filecon = (TextView)findViewById(R.id.filecon); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String filename = fname.getText().toString(); String filecontent = fcontent.getText().toString(); FileOperations fop = new FileOperations(); fop.write(filename, filecontent); if(fop.write(filename, filecontent)){ Toast.makeText(getApplicationContext(), filename+".txt created", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "I/O error", Toast.LENGTH_SHORT).show(); } } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String readfilename = fnameread.getText().toString(); FileOperations fop = new FileOperations(); String text = fop.read(readfilename); if(text != null){ filecon.setText(text); } else { Toast.makeText(getApplicationContext(), "File not Found", Toast.LENGTH_SHORT).show(); filecon.setText(null); } } }); } } |
FileOperations.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 |
package com.example.files; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import android.util.Log; public class FileOperations { public FileOperations() { } public Boolean write(String fname, String fcontent){ try { String fpath = "/sdcard/"+fname+".txt"; File file = new File(fpath); // If file does not exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(fcontent); bw.close(); Log.d("Suceess","Sucess"); return true; } catch (IOException e) { e.printStackTrace(); return false; } } public String read(String fname){ BufferedReader br = null; String response = null; try { StringBuffer output = new StringBuffer(); String fpath = "/sdcard/"+fname+".txt"; br = new BufferedReader(new FileReader(fpath)); String line = ""; while ((line = br.readLine()) != null) { output.append(line +"n"); } response = output.toString(); } catch (IOException e) { e.printStackTrace(); return null; } return response; } } |
Activity_files.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 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center" tools:context=".MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textAlignment="center" android:text="Android Read/Write File" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/fname" android:hint="File Name" /> <EditText android:layout_width="fill_parent" android:layout_height="100px" android:id="@+id/ftext" android:hint="File Text" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btnwrite" android:text="Write File" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/fnameread" android:hint="File Name" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btnread" android:text="Read File" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/filecon" /> </LinearLayout> |
Manifestfile.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 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.files" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".FilesActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |