Example usage for android.os Environment MEDIA_MOUNTED

List of usage examples for android.os Environment MEDIA_MOUNTED

Introduction

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

Prototype

String MEDIA_MOUNTED

To view the source code for android.os Environment MEDIA_MOUNTED.

Click Source Link

Document

Storage state if the media is present and mounted at its mount point with read/write access.

Usage

From source file:com.lxh.util.image.OtherUtils.java

/**
 * ??/*from  w w w.  j  a v a2  s . c  om*/
 * 
 * <pre>
 * SDCard
 * 
 * </pre>
 * 
 * @param context android.content.Context
 * @param dirName ???
 * @return APPapp_cache_path/dirName
 */
public static String getDiskCacheDir(Context context, String dirName) {
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File externalCacheDir = context.getExternalCacheDir();
        if (externalCacheDir != null) {
            cachePath = externalCacheDir.getPath();
        }
    }
    if (cachePath == null) {
        File cacheDir = context.getCacheDir();
        if (cacheDir != null && cacheDir.exists()) {
            cachePath = cacheDir.getPath();
        }
    }
    return cachePath + File.separator + dirName;
}

From source file:com.manning.androidhacks.hack037.MainActivity.java

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

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {/*from w w w .  j a  va2  s .co  m*/
        // 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
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }

    return mExternalStorageAvailable && mExternalStorageWriteable;
}

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

public static File getStorageFile(Application app, String saveFolder, String filename) throws StorageException {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        StringBuilder builder = new StringBuilder();
        builder.append(saveFolder);/*from   w  w w.  j  av  a2  s  .  c o m*/
        builder.append("/");
        builder.append(ROOT_FOLDER_APP);
        builder.append("/");
        builder.append(((CmisApp) app).getRepository().getServer().getName());
        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:cn.sharesdk.analysis.util.PreferencesHelper.java

/**
 * save events' info to cached file/*from   w ww. jav  a 2s. c o  m*/
 */
@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 ww w  .  j  av  a 2 s  .co  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 2s  . co 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

/**
 * ?/*from w  w  w  .j a  va2  s  .  c o m*/
 * @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;//from  w w  w.  j a  v a  2s  .c  o m
    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;
    }/* w w w .j  a v a2 s.  c o  m*/
    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  ww  . j a v a  2s  .com*/

    // 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;
}