Example usage for android.os Environment getExternalStorageState

List of usage examples for android.os Environment getExternalStorageState

Introduction

In this page you can find the example usage for android.os Environment getExternalStorageState.

Prototype

public static String getExternalStorageState() 

Source Link

Document

Returns the current state of the primary shared/external storage media.

Usage

From source file:de.fmaul.android.cmis.utils.StorageUtils.java

public static File getStorageFile(Application app, String repoId, String storageType, String itemId,
        String filename) throws StorageException {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        StringBuilder builder = new StringBuilder();
        builder.append(Environment.getExternalStorageDirectory());
        builder.append("/");
        builder.append("Android");
        builder.append("/");
        builder.append("data");
        builder.append("/");
        builder.append(app.getPackageName());
        builder.append("/");
        if (storageType != null) {
            builder.append("/");
            builder.append(storageType);
        }// w ww .  j  av a 2s .c  om
        if (repoId != null) {
            builder.append("/");
            builder.append(repoId);
        }
        if (itemId != null) {
            builder.append("/");
            builder.append(itemId.replaceAll(":", "_"));
        }
        if (filename != null) {
            builder.append("/");
            builder.append(filename);
        }
        return new File(builder.toString());
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        throw new StorageException("Storage in Read Only Mode");
    } else {
        throw new StorageException("Storage is unavailable");
    }
}

From source file:com.kynetx.api.java

private boolean isReadable() {
    boolean mExternalStorageAvailable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
    }/*ww  w . jav  a  2 s.  c  om*/
    return mExternalStorageAvailable;
}

From source file:de.petermoesenthin.alarming.util.FileUtil.java

/**
 * Checks if the external storage is readable
 *
 * @return/*from  ww  w .  j ava2  s . c  o  m*/
 */
public static boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

From source file:com.lillicoder.newsblurry.util.FileLogger.java

/**
 * Determines if the external storage media is available for writing.
 * @return <code>true</code> if external storage is available for writing,
 *          <code>false</code> otherwise.
 *//*from   w  w w. j a v a 2s  .  c om*/
private boolean isExternalStorageAvailable() {
    String externalMediaState = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(externalMediaState);
}

From source file:com.urremote.classifier.common.ExceptionHandler.java

private void writeToFile(String stacktrace, String filename) {
    try {//from www  .ja  v a  2 s  . c  om
        File outputFile = new File(Constants.PATH_SD_CARD_APP_LOC + File.separator + filename);

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.e(Constants.TAG, "Unable to write stacktrace to file. External media not mounted!");
            return;
        }

        Log.v(Constants.TAG, "Output Trace File:" + outputFile);
        if (!outputFile.getParentFile().exists()) {
            if (!outputFile.getParentFile().mkdirs()) {
                Log.e(Constants.TAG, "Unable to create log file directory:\n" + outputFile.getParentFile()
                        + "\nCaused when writing stack to file:\n" + stacktrace);
            }
        }
        if (!outputFile.exists()) {
            if (!outputFile.createNewFile()) {
                Log.e(Constants.TAG, "Unable to create log file:\n" + outputFile.getParentFile()
                        + "\nCaused when writing stack to file:\n" + stacktrace);
            }
        }
        BufferedWriter bos = new BufferedWriter(new FileWriter(outputFile));
        bos.write(stacktrace);
        bos.flush();
        bos.close();
        Log.e(Constants.TAG, stacktrace);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.binomed.showtime.android.util.images.ImageDownloader.java

private InputStream writeFile(String url, InputStream inputStream) {
    InputStream returnInputStream = inputStream;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        try {//from  w  w w.  j ava 2s  .co m
            File root = Environment.getExternalStorageDirectory();
            File imageFile = new File(root,
                    new StringBuilder(CineShowtimeCst.FOLDER_POSTER).append(getFileName(url)).toString());
            if (!imageFile.getParentFile().exists()) {
                imageFile.mkdirs();
            }
            if (!imageFile.exists()) {
                imageFile.createNewFile();
                FileOutputStream fileOutPut = new FileOutputStream(imageFile);
                byte[] tempon = new byte[10240];

                while (true) {
                    int nRead = inputStream.read(tempon, 0, tempon.length);
                    if (nRead <= 0) {
                        break;
                    }
                    fileOutPut.write(tempon, 0, nRead);
                }
                fileOutPut.close();
                returnInputStream = new FileInputStream(imageFile);
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error creating file", e);
        }
    }
    return returnInputStream;
}

From source file:com.kg.emailalbum.mobile.util.CacheManager.java

public File getOutboxDir() {
    String subdir = "created";
    // Default root is the application context internal files dir.
    File result = new File(mContext.getFilesDir(), subdir);

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        // If an external storage is available, use it as it will prevent
        // from overloading the internal memory.
        result = new File(Environment.getExternalStorageDirectory(), "data/EmailAlbum/" + subdir);
    }/* w w  w .j  ava2s.  c  o m*/

    // Physically create the directory (and its parents) if it does not
    // exist.
    if (!result.exists()) {
        result.mkdirs();
    }

    // Log.i(LOG_TAG, "Using dir " + result + " for cache");
    return result;
}

From source file:com.snipme.download.ImageDownloader.java

public static String saveToSdCard(Bitmap bitmap, String filename) {

    String stored = null;//from  w w w .j av  a 2 s  .  co m
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "SnipMe");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        Log.i("DownloadImage", "Create Folder Snipme");
        if (!mediaStorageDir.mkdirs()) {
            Log.d("DownloadImage", "failed to create directory SnipMe");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File file = new File(mediaStorageDir.getPath() + File.separator + filename + "_" + timeStamp + ".png");

    //File sdcard = Environment.getExternalStorageDirectory();

    //File folder = new File(sdcard.getAbsoluteFile(), "SnipMe");// the
    // dot
    // makes
    // this
    // directory
    // hidden
    // to
    // the
    // user
    //folder.mkdir();
    //File file = new File(folder.getAbsoluteFile(), filename + ".png");
    if (file.exists())
        return stored;

    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
        stored = "success";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stored;
}

From source file:jahirfiquitiva.iconshowcase.utilities.ZooperIconFontsHelper.java

private Subscription subscription(final int state) {
    if (state == FIXING)
        try {/*from  ww w  .  ja  va2s .com*/
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                throw new IOException("External Media is not mounted!");
        } catch (IOException exception) {
            if (this.mCallback != null)
                this.mCallback.OnError(exception);
        }

    Observable<?> o = Observable.create(this.onSubscribe(state)).subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    if (state != CHECKING)
        o.take(state == FIXING ? this.mMissing.size() : this.mFixed.size() + 1);

    if (this.mSubscription != null) {
        this.mSubscription.unsubscribe();
        this.mSubscription = null;
    }

    return this.mSubscription = o.subscribe(new Observer<Object>() {
        final ZooperIconFontsHelper c = ZooperIconFontsHelper.this;

        @Override
        public void onCompleted() {
            int s;
            if (state != CHECKING)
                s = state == FIXING ? c.mFixIndex : c.mCancelIndex - 1;
            else
                s = c.mMissing.size();
            if (c.mCheckAndFix && s > 0)
                c.fix();
            if (c.mCallback != null)
                c.mCallback.OnFinished(state, s);
        }

        @Override
        public void onError(Throwable throwable) {
            if (c.mCallback != null)
                c.mCallback.OnError(throwable);
        }

        @Override
        public void onNext(Object object) {
            if (object instanceof File) {
                if (c.mCallback != null)
                    c.mCallback.OnSaved((File) object);
            } else if (object instanceof String && state == CANCELING) {
                if (c.mCallback != null)
                    c.mCallback.OnDeleted((String) object);
            }
        }
    });
}

From source file:com.lazy.gank.logging.Logcat.java

public static Config defaultConfig() {
    Builder builder = newBuilder();/*w  ww. ja v  a2  s  .c om*/

    // ???
    do {
        String state = Environment.getExternalStorageState();
        //  SD ?
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            Log.w(TAG, "Not mount SD card!");
            break;
        }

        // SD ???
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            Log.w(TAG, "Not allow write SD card!");
            break;
        }

        File externalCacheDir = mContext.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir != null) {
            builder.logSavePath = externalCacheDir.getAbsolutePath() + FILE_SEPARATOR + DEFAULT_LOG_DIR;
        } else {
            Log.e(TAG, "externalCacheDir is null!");
            builder.fileLogLevel(OPERATION_BIT);
            break;
        }

        // ? SD ?????
        // ???? ? SD ? Cache/Log 
        String strSaveLogPath = builder.logSavePath;

        checkSaveLogPath(strSaveLogPath);
    } while (false);

    Config config = new Config(builder);
    return config;
}