Facebook
From Abrupt Duck, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 135
  1. package com.example.app8;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStreamWriter;
  13. import android.os.Environment;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19. public class MainActivity extends Activity {
  20.         Button save,load;
  21.         EditText message;
  22.         TextView t1;
  23.         String Message1;
  24.         @Override
  25.         protected void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.                 setContentView(R.layout.activity_main);
  28. save=(Button)findViewById(R.id.button1);
  29. load=(Button)findViewById(R.id.button2);
  30. message=(EditText)findViewById(R.id.editText1);
  31. t1=(TextView)findViewById(R.id.textView1);
  32.                                 save.setOnClickListener(new View.OnClickListener(){
  33.                                 public void onClick(View v){
  34.                                 //Get message from user store in message1 variable
  35.                                 Message1 =message.getText().toString();
  36.                                 try{
  37.                                 //Create a new folder called MyDirectory in SDCard
  38.                 File  sdcard=Environment.getExternalStorageDirectory();
  39. File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
  40.                                 directory.mkdirs();
  41.                                 //Create a new file name textfile.txt inside MyDirectory
  42.                                 File file=new File(directory,"textfile.txt");
  43.                        
  44.                 FileOutputStream fou=new FileOutputStream(file);
  45.                         OutputStreamWriter osw=new OutputStreamWriter(fou);
  46.                                 try{
  47.                                 //write a user data to file
  48.                                 osw.append(Message1);
  49.                                 osw.flush();
  50.                                 osw.close();
  51. Toast.makeText(getBaseContext(),"Datasaved",Toast.LENGTH_LONG).show();
  52.                                 }catch(IOException e){
  53.                                 e.printStackTrace();
  54.                                                                 }
  55.                                 }catch (FileNotFoundException e){
  56.                                 e.printStackTrace();
  57.                                 }
  58.                                 }
  59.                                 });
  60.                                 load.setOnClickListener(new View.OnClickListener(){
  61.                                 public void onClick(View v){
  62.                                 try{
  63.                                 File sdcard=Environment.getExternalStorageDirectory();
  64.                                 File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
  65.                                 File file=new File(directory,"textfile.txt");
  66.                                 FileInputStream fis=new FileInputStream(file);
  67.                                 InputStreamReader isr=new InputStreamReader(fis);
  68.                                 char[] data=new char[100];
  69.                                 String final_data="";
  70.                                 int size;
  71.                                 try{
  72.                                 while((size=isr.read(data))>0)
  73.                                 {
  74.                                 //read a data from file
  75.                                 String read_data=String.copyValueOf(data,0,size);
  76.                                 final_data+=read_data;
  77.                                 data=new char[100];
  78.                                 }
  79.                                 //display the data in output
  80.                                 Toast.makeText(getBaseContext(),"Message:"+final_data,Toast.LENGTH_LONG).show()
  81.                                 ;
  82.                                 }catch(IOException e){
  83.                                 e.printStackTrace();
  84.                                 }
  85.                                 }catch (FileNotFoundException e){
  86.                                 e.printStackTrace();
  87.                                 }
  88.                                 }
  89.                                 });
  90.                                 }
  91.                        
  92.         @Override
  93.         public boolean onCreateOptionsMenu(Menu menu) {
  94.                 // Inflate the menu; this adds items to the action bar if it is present.
  95.                 getMenuInflater().inflate(R.menu.main, menu);
  96.                 return true;
  97.         }
  98.  
  99. }
  100.  
  101.  
  102.  
  103. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  104.     xmlns:tools="http://schemas.android.com/tools"
  105.     android:layout_width="match_parent"
  106.     android:layout_height="match_parent"
  107.     android:paddingBottom="@dimen/activity_vertical_margin"
  108.     android:paddingLeft="@dimen/activity_horizontal_margin"
  109.     android:paddingRight="@dimen/activity_horizontal_margin"
  110.     android:paddingTop="@dimen/activity_vertical_margin"
  111.     tools:context=".MainActivity" >
  112.  
  113.    
  114.     <EditText
  115.         android:id="@+id/editText1"
  116.         android:layout_width="wrap_content"
  117.         android:layout_height="wrap_content"
  118.         android:layout_alignParentTop="true"
  119.         android:layout_centerHorizontal="true"
  120.         android:layout_marginTop="32dp"
  121.         android:ems="10" android:inputType="text">
  122.  
  123.         <requestFocus />
  124.     </EditText>
  125.  
  126.     <Button
  127.         android:id="@+id/button1"
  128.         android:layout_width="wrap_content"
  129.         android:layout_height="wrap_content"
  130.         android:layout_alignParentLeft="true"
  131.         android:layout_below="@+id/editText1"
  132.         android:layout_marginTop="41dp"
  133.         android:text="SAVEDATA"/>
  134.  
  135.     <Button
  136.         android:id="@+id/button2"
  137.         android:layout_width="wrap_content"
  138.         android:layout_height="wrap_content"
  139.         android:layout_alignBaseline="@+id/button1"
  140.         android:layout_alignBottom="@+id/button1"
  141.         android:layout_alignParentRight="true"
  142.         android:text="SHOWDATA"/>
  143.  
  144.     <TextView
  145.         android:id="@+id/textView1"
  146.         android:layout_width="wrap_content"
  147.         android:layout_height="wrap_content"
  148.         android:layout_below="@+id/linearLayout1"
  149.         android:layout_centerHorizontal="true"/>
  150.  
  151. </RelativeLayout>
  152.  
  153.  
  154.  
  155.  
  156. <?xml version="1.0" encoding="utf-8"?>
  157. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  158.     package="com.example.app8"
  159.     android:versionCode="1"
  160.     android:versionName="1.0" >
  161.  
  162.     <uses-sdk
  163.         android:minSdkVersion="8"
  164.         android:targetSdkVersion="17" />
  165. <uses-permission
  166. android:name="android.permission.WRITE_EXTERNAL_STORAGE">
  167.   </uses-permission>
  168.     <application
  169.         android:allowBackup="true"
  170.         android:icon="@drawable/ic_launcher"
  171.         android:label="@string/app_name"
  172.         android:theme="@style/AppTheme" >
  173.         <activity
  174.             android:name="com.example.app8.MainActivity"
  175.             android:label="@string/app_name" >
  176.             <intent-filter>
  177.                 <action android:name="android.intent.action.MAIN" />
  178.  
  179.                 <category android:name="android.intent.category.LAUNCHER" />
  180.             </intent-filter>
  181.         </activity>
  182.     </application>
  183.  
  184. </manifest>
  185.