has writable Storage - Android android.os

Android examples for android.os:Disk

Description

has writable Storage

Demo Code

import java.io.File;

import android.os.Environment;

public class Main {

  private static final String TAG = "";

  static public boolean hasStorage(boolean requireWriteAccess) {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
      if (requireWriteAccess) {
        boolean writable = checkFsWritable();
        return writable;
      } else {/*w  ww.  j  a v a2  s . com*/
        return true;
      }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
      return true;
    }
    return false;
  }

  private static boolean checkFsWritable() {
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
      if (!directory.mkdirs()) {
        return false;
      }
    }
    return directory.canWrite();
  }

}

Related Tutorials