External Storage

MainAcivity.java
public class MainActivity extends AppCompatActivity {
  private static final int PERMISION_WRITE_EXTERNAL_STORAGE = 123;
  String FileName = "myFile";
  Button BtnSave, BtnRead;
  EditText editName;
  TextView readName;
  String name;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   BtnSave = (Button) findViewById(R.id.idBtnSave);
   BtnSave.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View v) {
     int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
    
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
      saveFile();
     
    } else {
      ActivityCompat.requestPermissions(MainActivity.this, new String[] {
      android.Manifest.permission.WRITE_EXTERNAL_STORAGE
     }, PERMISION_WRITE_EXTERNAL_STORAGE);
     
    }
    
   }
   
  });
   BtnRead = (Button) findViewById(R.id.idBtnRead);
   BtnRead.setOnClickListener(new View.OnClickListener() {
   @Override 
    public void onClick(View v) {
     readFile();
   }
  });
   editName = (EditText) findViewById(R.id.idName);
   readName = (TextView) findViewById(R.id.idReadtxt);
  
 }
  @Override 
   public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  
  switch (requestCode) {
   
   case PERMISION_WRITE_EXTERNAL_STORAGE:
    {
      //premission to read storage 
     
     if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
       saveFile();
      
     } else {
       Toast.makeText(MainActivity.this, "Please grant Permission to Save File", Toast.LENGTH_SHORT).show();
        }
     
     return;
    }
  }
 }
 private void readFile() {
 
  try {
   {
     //To Read the File in a Directory called MyData in the root ... 
     //UnComment and Replace the Code 
    
    /* File myDir = new File(Environment.getExternalStorageDirectory() + "/MyData"); 
     myDir.mkdirs(); 
     File file = new File(myDir,FileName);*/
     //To Read the File directly in root 
     File file = new File(Environment.getExternalStorageDirectory(), FileName);
     //Below Code will remain the same 
     FileInputStream fin = new FileInputStream(file);
     InputStreamReader inputStream = new InputStreamReader(fin);
     BufferedReader bufferedReader = new BufferedReader(inputStream);
     StringBuilder stringBuilder = new StringBuilder();
     String line = null;
    
    while ((line = bufferedReader.readLine()) != null) {
      stringBuilder.append(line);
    }
     fin.close();
     inputStream.close();
     readName.setText("Name: " + stringBuilder.toString());
     Toast.makeText(this, "Data Retrieved :" + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
    
   } catch (java.io.IOException e) {
     e.printStackTrace();
    
   }
  }
  private void saveFile() {
   
   try {
     //To Save the File in a Directory called MyData in the root... 
     //UnComment and Replace the Code 
     /* File myDir = new File(Environment.getExternalStorageDirectory() + "/MyData"); 91 myDir.mkdirs(); 92 File file = new File(myDir,FileName);*/ 93​ 94 //To Save the File directly in root 
     File file = new File(Environment.getExternalStorageDirectory(), FileName);
     //Below Code will remain the same 
     FileOutputStream fos = new FileOutputStream(file);
     name = editName.getText().toString();
     fos.write(name.getBytes());
     fos.close();
     Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
    
   } catch (java.io.IOException e) {
     e.printStackTrace();
   }
  }
 }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_main" 
     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"
     android:orientation="vertical"
     tools:context="com.example.arjun.storage.MainActivity"> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:hint="Name:" 
     android:ems="10" 
     android:id="@+id/idName" />
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
        <Button 
         android:text="Save To File"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_margin="5dp" 
         android:id="@+id/idBtnSave" />
        <Button 
         android:text="Read File" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_margin="5dp" 
         android:id="@+id/idBtnRead" />  
    </LinearLayout>  
    <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:textColor="#000000" 
     android:id="@+id/idReadtxt"/> 
</LinearLayout>  

Menifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Share this