Articles → Android → Interaction With External Storage In Android

Interaction With External Storage In Android





  1. Create a file in external storage.
  2. Write in the external storage file.
  3. Read data from external storage file.
  4. Delete the external storage file.

Example




  1. An edit text control
  2. A button ‘Write’ for writing data from edit text to external storage.
  3. A button ‘Clear’ to clear data from edit text.
  4. A button ‘Read’ for reading data from external storage and display it in edit text.
  5. A button ‘Delete’ for deleting the file in external storage.


@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  externalFile = getDocumentDir("file.txt");

  if (!externalFile.exists()) {
    try {
      externalFile.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

File externalFile;

public void WriteInExternalStorage(View view) {
  EditText name = (EditText) findViewById(R.id.name);
  String data = name.getText().toString();

  try {
    if (!isExternalStorageWritable()) {
      Toast.makeText(this, "External storage is not writable", Toast.LENGTH_SHORT).show();
    }
    FileOutputStream outputFileStream = new FileOutputStream(externalFile);
    outputFileStream.write(data.getBytes());
    outputFileStream.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

public void ClearData(View view) {
  EditText name = (EditText) findViewById(R.id.name);
  name.setText("");
}

public void DeleteExternalStorageFile(View view) {
  try {
    boolean deleted = externalFile.delete();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

public void ReadFromExternalStorage(View view) {
  EditText name = (EditText) findViewById(R.id.name);
  String myData = "";
  try {
    if (!isExternalStorageReadable()) {
      Toast.makeText(this, "External storage is not readable", Toast.LENGTH_SHORT).show();
    }
    FileInputStream fis = new FileInputStream(externalFile);
    DataInputStream in = new DataInputStream(fis);
    BufferedReader br =
      new BufferedReader(new InputStreamReader( in ));
    String strLine;
    while ((strLine = br.readLine()) != null) {
      myData = myData + strLine;
    } in .close();
    name.setText(myData);
  } catch (Exception e) {
    Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
    e.printStackTrace();
  }
}

public boolean isExternalStorageReadable() {
  String state = Environment.getExternalStorageState();

  if (Environment.MEDIA_MOUNTED.equals(state) ||
    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    return true;
  }

  return false;
}

public boolean isExternalStorageWritable() {
  String state = Environment.getExternalStorageState();

  if (Environment.MEDIA_MOUNTED.equals(state)) {
    return true;
  }

  return false;
}

public File getDocumentDir(String name) {
  String dir = Environment.getExternalStorageDirectory() + File.separator + "text";
  //create folder
  File folder = new File(dir); //folder name
  folder.mkdirs();

  //create file
  File file = new File(dir, name);
  return file;
}



Permissions




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





Posted By  -  Karan Gupta
 
Posted On  -  Monday, January 6, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250