Android Open Source - base-android-utils External Storage






From Project

Back to project page base-android-utils.

License

The source code is released under:

Apache License

If you think the Android project base-android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.pc.mobile.helper.v14.files;
//from   w  w w  .ja  va 2 s.co  m
import android.os.Environment;

import java.io.File;
import java.io.IOException;

public class ExternalStorage {

  private ExternalStorage() {}

  /**
   * Creates the File instance where the data is persisted in *external
   * storage* along with user files (debugging purposes *but not in
   * production*). The files will NOT be removed on application uninstall - so
   * use this while installing and uninstalling to keep the logs around. If
   * the external folder is not available or if the directory can not be
   * created an exception is thrown forcing the caller to deal with it.
   * External storage must be used sparingly
   *
   * @param rootDir
   *            the directory the file will be created relative to the
   *            directory returned depending on the value of
   *            {@code dirInPublicStorage} - do not give an absolute path
   * @param filename
   * @param dirInPublicStorage
   *            specifies the root relative to which the {@code rootDir}/
   *            {@code filename} path is relative. If this is one of
   *            DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES,
   *            DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES,
   *            DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM then
   *            the relevant directory is returned (as per the docs of
   *            {@code getExternalStoragePublicDirectory()}). If it is null
   *            then the root of external storage is returned as per
   *            {@code getExternalStorageDirectory()}. See the section
   *            "Accessing files on external storage" in {@link http
   *            ://developer.android.com/guide/topics/data/data-storage.html}
   * @return the File instance corresponding to the rootPath/filename relative
   *         to External Files dir for the application storage or null on
   *         failure
   * @throws IOException
   *             if the external storage is unavailable or the dir can't be
   *             created
   */
  public static File fileExternalPublicStorage(String rootDir,
      String filename, String dirInPublicStorage)
      throws IOException {
    // create a File object for the parent directory
    if (isExternalStoragePresent()) {
      File logdir;
      if (dirInPublicStorage != null) {
        logdir = new File(Environment
          .getExternalStoragePublicDirectory(dirInPublicStorage)
          .getAbsolutePath()
          + File.separator + rootDir);
      } else {
        logdir = new File(Environment.getExternalStorageDirectory()
          .getAbsolutePath() + File.separator + rootDir);
      }
      // have the object build the directory structure, if needed.
      if (FileUtils.createDir(logdir)) {
        // create a File *instance* for the output file - can't fail
        return new File(logdir, filename);
      }
      throw new IOException("Can not create folder "
        + logdir.getAbsolutePath());
    }
    throw new IOException("External storage not present or not writable");
  }

  /**
   * Wrapper around {@link Environment#getExternalStorageState()}.
   *
   * @return true if External Storage is both available and writable
   * @see <a
   *      href=http://developer.android.com/guide/topics/data/data-storage.html
   *      #filesExternal>Using the External Storage</a>
   */
  private static boolean isExternalStoragePresent() {
    boolean externalStorageAvailable = false;
    boolean externalStorageWriteable = false;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      // We can read and write the media
      externalStorageAvailable = externalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
      // We can only read the media
      externalStorageAvailable = true;
      externalStorageWriteable = false;
    } else {
      // Something else is wrong. It may be one of many other states, but
      // all we need to know is we can neither read nor write
      externalStorageAvailable = externalStorageWriteable = false;
    }
    return (externalStorageAvailable) && (externalStorageWriteable);
  }
}




Java Source Code List

me.pc.mobile.helper.v14.BuildConfig.java
me.pc.mobile.helper.v14.base.BaseActivity.java
me.pc.mobile.helper.v14.base.BaseApp.java
me.pc.mobile.helper.v14.base.BaseFrag.java
me.pc.mobile.helper.v14.base.abs.BaseJsonParser.java
me.pc.mobile.helper.v14.crypt.AES7Padding.java
me.pc.mobile.helper.v14.crypt.AES.java
me.pc.mobile.helper.v14.crypt.Base64.java
me.pc.mobile.helper.v14.crypt.CheckUtils.java
me.pc.mobile.helper.v14.crypt.ConfigureEncryptAndDecrypt.java
me.pc.mobile.helper.v14.crypt.RSA.java
me.pc.mobile.helper.v14.files.ExternalStorage.java
me.pc.mobile.helper.v14.files.FileUtils.java
me.pc.mobile.helper.v14.files.Reader.java
me.pc.mobile.helper.v14.files.Writer.java
me.pc.mobile.helper.v14.http.AsyncHttpUtil.java
me.pc.mobile.helper.v14.net.Addresses.java
me.pc.mobile.helper.v14.net.NetworkUtil.java
me.pc.mobile.helper.v14.net.WifiWaker.java
me.pc.mobile.helper.v14.receiver.BatteryStateReceiver.java
me.pc.mobile.helper.v14.receiver.NetworkStateChangeReceiver.java
me.pc.mobile.helper.v14.ui.image.RoundedDrawable.java
me.pc.mobile.helper.v14.ui.image.RoundedImageView.java
me.pc.mobile.helper.v14.util.AppInstallUtil.java
me.pc.mobile.helper.v14.util.BitDrawableUtil.java
me.pc.mobile.helper.v14.util.DeviceIdentifier.java
me.pc.mobile.helper.v14.util.DisplayUtils.java
me.pc.mobile.helper.v14.util.IntentUtil.java
me.pc.mobile.helper.v14.util.IoUtils.java
me.pc.mobile.helper.v14.util.LogUtil.java
me.pc.mobile.helper.v14.util.PackageUtil.java
me.pc.mobile.helper.v14.util.PermissionAssertUtils.java
me.pc.mobile.helper.v14.util.RegexUtil.java
me.pc.mobile.helper.v14.util.SharedPrefUtil.java
me.pc.mobile.helper.v14.util.StorageUtils.java