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:cn.sharesdk.analysis.util.PreferencesHelper.java

/**
 * save events' info to cached file/*  w  w w. j a v a2s.c om*/
 */
@Deprecated
public void saveInfoToFile(String key, JSONObject object) {
    JSONObject existJSON = null;
    try {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && DeviceHelper
                .getInstance(context).checkPermissions("android.permission.WRITE_EXTERNAL_STORAGE")) {
            // Does a cached file exist
            File cacheRoot = new File(getSdcardPath(), packageName);
            if (!cacheRoot.exists()) {
                cacheRoot.mkdirs();
                Ln.i("cacheRoot path", "no path");
            }

            File cacheFile = new File(cacheRoot, "mobclick_agent_cached_" + packageName);
            if (!cacheFile.exists()) {
                cacheFile.createNewFile();
                Ln.i("cacheFile path", "no path createNewFile");
            }

            // Does any data in the cached file
            FileInputStream in = new FileInputStream(cacheFile);
            StringBuffer sb = new StringBuffer();

            int i = 0;
            byte[] s = new byte[1024 * 4];

            while ((i = in.read(s)) != -1) {
                sb.append(new String(s, 0, i));
            }
            in.close();

            if (sb.length() != 0) {
                existJSON = new JSONObject(sb.toString());
            } else {
                existJSON = new JSONObject();
            }

            if (existJSON.has(key)) {
                JSONArray newDataArray = existJSON.getJSONArray(key);
                Ln.i("SaveInfo", object + "");
                newDataArray.put(object);
            } else {
                JSONArray newArray = new JSONArray();
                newArray.put(0, object);
                existJSON.put(key, newArray);
                Ln.i("SaveInfo", "jsonobject" + existJSON);
            }

            Ln.i("SaveInfo", "save json data to the cached file!");
            // save json data to the cached file
            FileOutputStream fileOutputStream = new FileOutputStream(cacheFile, false);
            fileOutputStream.write(existJSON.toString().getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:org.zywx.wbpalmstar.engine.EDownloadDialog.java

@Override
public void run() {
    try {//from   w  ww .  j  a va 2s.  c o m
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            mProgressHandler.sendMessage(mProgressHandler.obtainMessage(-2, "SD?!"));
            mProgressHandler.sendEmptyMessage(-3);
            return;
        }
        mClient = new URL(url);
        mConnection = (HttpURLConnection) mClient.openConnection();
        mConnection.setRequestMethod("GET");
        mConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        String cookie = getCookie(url);
        if (null != cookie) {
            mConnection.setRequestProperty(SM.COOKIE, cookie);
        }
        mConnection.setUseCaches(false);
        mConnection.setRequestProperty("Connection", "Keep-Alive");
        mConnection.setRequestProperty("Charset", HTTP.UTF_8);
        mConnection.setRequestProperty("User-Agent", EBrowserSetting.USERAGENT);
        mConnection.setReadTimeout(1000 * 30);
        mConnection.setConnectTimeout(1000 * 30);
        mConnection.setInstanceFollowRedirects(false);
        mConnection.connect();
        int responseCode = mConnection.getResponseCode();
        if (200 == responseCode) {
            saveToFile();
        } else {
            mProgressHandler.sendMessage(mProgressHandler.obtainMessage(-2, ",?!"));
            mProgressHandler.sendEmptyMessage(-3);
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (!mFromStop) {
            mProgressHandler.sendMessage(mProgressHandler.obtainMessage(-2, ",?!"));
        }
        mProgressHandler.sendEmptyMessage(-3);
    }
}

From source file:com.connectsdk.device.DefaultConnectableDeviceStore.java

public DefaultConnectableDeviceStore(Context context) {
    String dirPath;/*from w  w w . j  a  v a 2  s .c o  m*/
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        dirPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
        dirPath = Environment.MEDIA_UNMOUNTED;
    }
    fileFullPath = dirPath + DIRPATH + FILENAME;

    try {
        fileFullPath = context.getPackageManager().getPackageInfo(context.getPackageName(),
                0).applicationInfo.dataDir + "/" + FILENAME;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    load();
}

From source file:com.ihelpoo.app.AppException.java

/**
 * ?/* w ww.  j a v a 2 s  .c  om*/
 * @param excp
 */
public void saveErrorLog(Exception excp) {
    String errorlog = "errorlog.txt";
    String savePath = "";
    String logFilePath = "";
    FileWriter fw = null;
    PrintWriter pw = null;
    try {
        //?SD?
        String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
            savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ihelpoo/Log/";
            File file = new File(savePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            logFilePath = savePath + errorlog;
        }
        //SD?
        if (logFilePath == "") {
            return;
        }
        File logFile = new File(logFilePath);
        if (!logFile.exists()) {
            logFile.createNewFile();
        }
        fw = new FileWriter(logFile, true);
        pw = new PrintWriter(fw);
        pw.println("--------------------" + (new Date().toLocaleString()) + "---------------------");
        excp.printStackTrace(pw);
        pw.close();
        fw.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
            }
        }
    }

}

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

private Bitmap getFileDrawable(String url) {
    Bitmap image = null;// w  ww .  ja  v  a 2 s .  c  om
    String finalFileName = url.substring(url.lastIndexOf("/"), url.length());
    try {
        File root = Environment.getExternalStorageDirectory();
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            File imageFile = new File(root,
                    new StringBuilder(CineShowtimeCst.FOLDER_POSTER).append(getFileName(url)).toString());
            Log.d(LOG_TAG, "Try getting file : " + imageFile.getAbsolutePath());
            if (imageFile.exists()) {
                image = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            }
        } else {
            Log.d(LOG_TAG, "SD card unmounted : " + url);

        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "Error creating file", e);
    }

    if (image != null) {
        addBitmapToCache(url, image);
    }

    return image;
}

From source file:com.kynetx.api.java

private boolean isWritable() {
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageWriteable = true;
    }/*from  w  w  w  .ja v a  2s  . c om*/
    return mExternalStorageWriteable;
}

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

public File getInboxDir() {
    String subdir = "received";
    // 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);
    }//from w w  w.  ja v a 2s.  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.polyvi.xface.util.XFileUtils.java

/**
 * ?sdcard/*  www  .  j  a v a 2s. c o  m*/
 *
 * @return sdcard??sdcard?null
 */
public static String getSdcardPath() {
    String path = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        path = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
        path = XExternalStorageScanner.getExternalStoragePath();
    }
    if (null == path) {
        XLog.w(CLASS_NAME, "No External Storage");
        return null;
    }
    File filePath = null;
    try {
        filePath = new File(path).getCanonicalFile();
    } catch (IOException e) {
        XLog.e(CLASS_NAME, e.getMessage());
        e.printStackTrace();
        return null;
    }
    return filePath.getAbsolutePath();
}

From source file:com.commontime.cordova.audio.AudioPlayer.java

/**
 * Constructor./*from   ww  w  .ja v a  2 s  . c  o m*/
 *
 * @param handler           The audio handler object
 * @param id                The id of this audio player
 */
public AudioPlayer(AudioHandler handler, String id, String file) {
    this.handler = handler;
    this.id = id;
    this.audioFile = file;
    this.recorder = new MediaRecorder();

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.m4a";
    } else {
        this.tempFile = "/data/data/" + handler.cordova.getActivity().getPackageName()
                + "/cache/tmprecording.m4a";
    }

}

From source file:ch.ethz.dcg.jukefox.commons.utils.AndroidUtils.java

public static boolean isSdCardOk() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        Log.w(TAG, "Access to sd-card is read only");
        return false;
    } else {//w  w  w. ja v a  2  s  .  c  o  m
        return false;
    }

}