Articles → Android → Interaction with external storage in android

Interaction with external storage in android














Example
















@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