Example usage for android.os Environment MEDIA_MOUNTED_READ_ONLY

List of usage examples for android.os Environment MEDIA_MOUNTED_READ_ONLY

Introduction

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

Prototype

String MEDIA_MOUNTED_READ_ONLY

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

Click Source Link

Document

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

Usage

From source file:org.uguess.android.sysinfo.SiragonManager.java

private String[] getExternalStorageInfo() {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED.equals(state)) {
        return getStorageInfo(Environment.getExternalStorageDirectory());
    }// ww w  . j av  a  2 s .  co  m

    return null;
}

From source file:se.lu.nateko.edca.svc.GeoHelper.java

/**
 * Checks the state of the external storage to determine
 * what interactions can be performed.//from ww  w.  ja v  a 2s .c  o  m
 */
private void updateExternalStorageState() {
    //      Log.d(TAG, "updateExternalStorageState() called.");
    String state = Environment.getExternalStorageState();

    /* Check the availability of the external storage. */
    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 // We can neither read nor write.          
        mExternalStorageAvailable = mExternalStorageWriteable = false;
}

From source file:net.fred.feedex.fragment.EditFeedsListFragment.java

private void exportToOpml() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
            || Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {

        new Thread(new Runnable() { // To not block the UI
            @Override//from w  w w.  ja  v  a 2  s  . c  om
            public void run() {
                try {
                    final String filename = Environment.getExternalStorageDirectory().toString() + "/Flym_"
                            + System.currentTimeMillis() + ".opml";

                    OPML.exportToFile(filename);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(),
                                    String.format(getString(R.string.message_exported_to), filename),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (Exception e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(), R.string.error_feed_export, Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }).start();
    } else {
        Toast.makeText(getActivity(), R.string.error_external_storage_not_available, Toast.LENGTH_LONG).show();
    }
}

From source file:com.viktorrudometkin.burramys.fragment.EditFeedsListFragment.java

private void exportToOpml() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
            || Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {

        new Thread(new Runnable() { // To not block the UI
            @Override/*  w  w  w .  ja  v  a2  s  .  c om*/
            public void run() {
                try {
                    final String filename = Environment.getExternalStorageDirectory().toString() + "/Burramys_"
                            + System.currentTimeMillis() + ".opml";

                    OPML.exportToFile(filename);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            UiUtils.showMessage(getActivity(),
                                    String.format(getString(R.string.message_exported_to), filename));
                        }
                    });
                } catch (Exception e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            UiUtils.showMessage(getActivity(), R.string.error_feed_export);
                        }
                    });
                }
            }
        }).start();
    } else {
        UiUtils.showMessage(getActivity(), R.string.error_external_storage_not_available);
    }
}

From source file:org.uguess.android.sysinfo.SiragonManager.java

private String[] getA2SDStorageInfo() {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED.equals(state)) {
        // here we just guess if it's app2sd enabled, this should work for
        // most app2sd enabled roms, but may not all.

        File f = new File("/dev/block/mmcblk0p2"); //$NON-NLS-1$

        if (f.exists()) {
            BufferedReader reader = null;
            String mountPoint = null;

            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(F_MOUNT_INFO)), 1024);

                String line;/*from w  w  w  .jav  a2 s  .c  om*/

                while ((line = reader.readLine()) != null) {
                    if (line.startsWith("/dev/block/mmcblk0p2 ")) //$NON-NLS-1$
                    {
                        // 21==length of the above string
                        int idx = line.indexOf(' ', 21);

                        if (idx != -1) {
                            mountPoint = line.substring(21, idx).trim();
                        }

                        break;
                    }
                }
            } catch (Exception e) {
                Log.e(SiragonManager.class.getName(), e.getLocalizedMessage(), e);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                        reader = null;
                    } catch (IOException ie) {
                        Log.e(SiragonManager.class.getName(), ie.getLocalizedMessage(), ie);
                    }
                }
            }

            if (mountPoint != null) {
                f = new File(mountPoint);

                if (f.exists() && f.isDirectory()) {
                    return getStorageInfo(f);
                }
            }
        }
    }

    return getSystemA2SDStorageInfo();
}

From source file:csh.cryptonite.Cryptonite.java

public static boolean externalStorageIsWritable() {
    /* Check sd card state */
    String state = Environment.getExternalStorageState();

    boolean extStorAvailable = false;
    boolean extStorWriteable = false;

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        extStorAvailable = extStorWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        extStorAvailable = true;//from  w w w.ja v  a  2 s .  c o  m
        extStorWriteable = 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
        extStorAvailable = extStorWriteable = false;
    }

    return extStorAvailable && extStorWriteable;
}

From source file:com.coincollection.MainActivity.java

/**
 * Begins the collection export process by doing some preliminary external media checks and
 * prompts the user if an export will overwrite previous backup files.
 *//*w ww . j  a v  a  2  s.  c om*/
private void handleExportCollectionsPart1() {
    // TODO Move this function to be more resistant to ANR, if reports show that it is a
    // problem

    // Check for WRITE_EXTERNAL_STORAGE permissions (must request starting in API Level 23)
    // hasPermissions() will kick off the permissions request and the handler will re-call
    // this method after prompting the user.
    if (!hasPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, EXPORT_PERMISSIONS_REQUEST)) {
        return;
    }

    // See whether we can write to the external storage
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // Should be able to write to it without issue
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // Can't write to it, so notify user
        showCancelableAlert(mRes.getString(R.string.cannot_wr_ext_media_ro));
        return;
    } else if (Environment.MEDIA_SHARED.equals(state)) {
        // Shared with PC so can't write to it
        showCancelableAlert(mRes.getString(R.string.cannot_wr_ext_media_shared));
        return;
    } else {
        // Doesn't exist, so notify user
        showCancelableAlert(mRes.getString(R.string.cannot_wr_ext_media_state, state));
        return;
    }

    //http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
    File sdCard = Environment.getExternalStorageDirectory();
    String path = sdCard.getAbsolutePath() + EXPORT_FOLDER_NAME;
    File dir = new File(path);

    if (dir.isDirectory() || dir.exists()) {
        // Let the user decide whether they want to delete this
        showExportConfirmation();
    } else {
        // Proceed with exporting directly
        handleExportCollectionsPart2();
    }
}

From source file:com.edible.ocr.CaptureActivity.java

/** Finds the proper location on the SD card where we can save files. */
private File getStorageDirectory() {
    //Log.d(TAG, "getStorageDirectory(): API level is " + Integer.valueOf(android.os.Build.VERSION.SDK_INT));

    String state = null;/* www  .  ja  va  2  s .  c o  m*/
    try {
        state = Environment.getExternalStorageState();
    } catch (RuntimeException e) {
        Log.e(TAG, "Is the SD card visible?", e);
        showErrorMessage("Error", "Required external storage (such as an SD card) is unavailable.");
    }

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

        // We can read and write the media
        //       if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 7) {
        // For Android 2.2 and above

        try {
            return getExternalFilesDir(Environment.MEDIA_MOUNTED);
        } catch (NullPointerException e) {
            // We get an error here if the SD card is visible, but full
            Log.e(TAG, "External storage is unavailable");
            showErrorMessage("Error", "Required external storage (such as an SD card) is full or unavailable.");
        }

        //        } else {
        //          // For Android 2.1 and below, explicitly give the path as, for example,
        //          // "/mnt/sdcard/Android/data/edu.sfsu.cs.orange.ocr/files/"
        //          return new File(Environment.getExternalStorageDirectory().toString() + File.separator + 
        //                  "Android" + File.separator + "data" + File.separator + getPackageName() + 
        //                  File.separator + "files" + File.separator);
        //        }

    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        Log.e(TAG, "External storage is read-only");
        showErrorMessage("Error",
                "Required external storage (such as an SD card) is unavailable for data storage.");
    } 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
        Log.e(TAG, "External storage is unavailable");
        showErrorMessage("Error",
                "Required external storage (such as an SD card) is unavailable or corrupted.");
    }
    return null;
}

From source file:com.piusvelte.taplock.client.core.TapLockSettings.java

private boolean copyFileToSDCard(String filename) {
    AssetManager assetManager = getAssets();
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
        Toast.makeText(TapLockSettings.this, R.string.msg_sdcardunavailable, Toast.LENGTH_SHORT).show();
    else {// ww  w  . ja  va2  s. c  o  m
        try {
            InputStream in = assetManager.open(filename);
            OutputStream out = new FileOutputStream(
                    Environment.getExternalStorageDirectory().getPath() + "/" + filename);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1)
                out.write(buffer, 0, read);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            return true;
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            Toast.makeText(TapLockSettings.this, R.string.msg_oops, Toast.LENGTH_SHORT).show();
        }
    }
    return false;
}