get Application Storage Dir - Android android.os

Android examples for android.os:Disk

Description

get Application Storage Dir

Demo Code

import java.io.File;

import android.content.ContextWrapper;
import android.os.Environment;

public class Main {

  public static File getApplicationStorageDir(ContextWrapper pContextWrapper) {

    File lApplicationDir = null;/* w  ww.ja va2s .  co  m*/
    if (isSdCardWritable()) {
      lApplicationDir = pContextWrapper.getExternalFilesDir(null);
    } else {
      lApplicationDir = pContextWrapper.getDir("data", ContextWrapper.MODE_WORLD_WRITEABLE);
    }
    return lApplicationDir;
  }

  public static boolean isSdCardWritable() {
    String lStorageState = Environment.getExternalStorageState();
    boolean lIsSdcardWritable;
    boolean lIsSdCardAvailable;

    if (Environment.MEDIA_MOUNTED.equals(lStorageState)) {
      lIsSdCardAvailable = lIsSdcardWritable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(lStorageState)) {
      lIsSdCardAvailable = true;
      lIsSdcardWritable = false;
    } else {
      lIsSdCardAvailable = lIsSdcardWritable = false;
    }
    return lIsSdCardAvailable && lIsSdcardWritable;
  }

}

Related Tutorials