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:com.hiqes.android.demopermissionsm.ui.LogLoadDialog.java

@NonNull
@Override//from  w ww .j ava 2 s .  co m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder bldr = new AlertDialog.Builder(getActivity());

    //  Make sure external storage is actually available, otherwise warn
    //  the user an get out.
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED.equals(state)) {
        Logger.d(TAG, "External storage available: " + state);

        //  Get the list of all files saved in our directory on external
        //  storage.  Use the generic external storage location to show
        //  the use of permissions.
        mLogDir = new File(Environment.getExternalStorageDirectory(), PROG_SAVE_DIR_NAME);
        String[] files = mLogDir.list();
        if ((files != null) && (files.length > 0)) {
            mNoFiles = false;
            Logger.d(TAG, "Found files: " + Arrays.toString(files));
        } else {
            files = new String[1];
            files[0] = getString(R.string.no_saved_files);
            Logger.d(TAG, files[0]);
        }

        mLogFileAdapter = new ArrayAdapter<>(getActivity(), R.layout.list_files, R.id.filename, files);
        bldr.setAdapter(mLogFileAdapter, this);
    } else {
        Logger.w(TAG, "No external storage mounted");
        bldr.setMessage(R.string.prog_load_unavail);
        bldr.setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LogLoadDialog.this.dismiss();
            }
        });
    }

    bldr.setTitle(R.string.prog_load_title);
    return bldr.create();
}

From source file:net.sileht.lullaby.Utils.java

public static void checkStorage() {
    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.ja  v a 2  s.c  om
        // 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;
    }
}

From source file:com.mpower.mintel.android.application.MIntel.java

/**
 * Creates required directories on the SDCard (or other external storage)
 * //from w  ww  .  j  a  v  a 2s.co  m
 * @throws RuntimeException
 *             if there is no SDCard or the directory exists as a non
 *             directory
 */
public static void createMIntelDirs() throws RuntimeException {
    String cardstatus = Environment.getExternalStorageState();
    if (cardstatus.equals(Environment.MEDIA_REMOVED) || cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
            || cardstatus.equals(Environment.MEDIA_UNMOUNTED)
            || cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)
            || cardstatus.equals(Environment.MEDIA_SHARED)) {
        RuntimeException e = new RuntimeException(
                "mIntel reports :: SDCard error: " + Environment.getExternalStorageState());
        throw e;
    }

    String[] dirs = { MINTEL_ROOT, FORMS_PATH, INSTANCES_PATH, CACHE_PATH, METADATA_PATH, PRESCRIPTION_PATH };

    for (String dirName : dirs) {
        File dir = new File(dirName);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                RuntimeException e = new RuntimeException(
                        "mIntel reports :: Cannot create directory: " + dirName);
                throw e;
            }
        } else {
            if (!dir.isDirectory()) {
                RuntimeException e = new RuntimeException(
                        "mIntel reports :: " + dirName + " exists, but is not a directory");
                throw e;
            }
        }
    }

    String[] fileNames = { "pres_n.ogg", "call_n.ogg" };
    copyAudioFiles(fileNames);
}

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

/**
 * Returns snapshot at index modulo the length of the list, if index is < 0, it returns a random snapshot
 *///from w w  w .java  2 s.  co  m
private static List<WifiReading> getSnapshot(int index, Context c) {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can at least read the external storage
        File root = c.getExternalCacheDir();
        File[] snapshotFiles = root.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String filename) {
                if (filename.endsWith(FILE_EXTENSION)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        if (snapshotFiles.length > 0) {
            if (index < 0) {
                Random rand = new Random();
                index = rand.nextInt(snapshotFiles.length);
            } else {
                index = index % snapshotFiles.length;
            }
            try {
                // read file into a string
                FileInputStream fstream = new FileInputStream(snapshotFiles[index]);
                Log.d(TAG, snapshotFiles[index].getName());
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String fileContents = "";
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    fileContents += strLine;
                }

                // make a json array out of the string
                JSONArray json = new JSONArray(fileContents);

                // parse the json array
                return jsonToReadings(json);
            } catch (Exception e) {
                Log.e(TAG, "Could not read file.");
                return null;
            }
        } else {
            // there are no cached snapshots
            return null;
        }
    } else {
        // we cannot read the external storage
        return null;
    }
}

From source file:org.proninyaroslav.libretorrent.core.utils.FileIOUtils.java

public static boolean isStorageReadable() {
    String state = Environment.getExternalStorageState();

    return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}

From source file:org.yammp.app.MediaPlayerActivity.java

@Override
public void onCreate(Bundle icicle) {

    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(icicle);
    mPrefs = new PreferencesEditor(this);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    setContentView(R.layout.main);//  ww  w  .j  a  v  a 2 s  .c om

    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    mActionBar.setDisplayShowTitleEnabled(false);

    String mount_state = Environment.getExternalStorageState();

    if (!Environment.MEDIA_MOUNTED.equals(mount_state)
            && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(mount_state)) {
        startActivity(new Intent(this, ScanningProgress.class));
        finish();
    }

    mAdapter = new PagesAdapter(mActionBar);
    mAdapter.addPage(MusicBrowserFragment.class, getString(R.string.label_music));
    mAdapter.addPage(VideoFragment.class, getString(R.string.label_video));
    mAdapter.addPage(MusicPlaybackFragment.class, getString(R.string.now_playing));

}

From source file:com.nks.nksmod.otaupdater.DownloadsActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String extState = Environment.getExternalStorageState();
    if (!extState.equals(Environment.MEDIA_MOUNTED) && !extState.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        Toast.makeText(this, extState.equals(Environment.MEDIA_SHARED) ? R.string.toast_nosd_shared
                : R.string.toast_nosd_error, Toast.LENGTH_LONG).show();
        finish();//from ww w  .  ja  v  a 2 s. c  o  m
    }

    setContentView(R.layout.downloads);

    dlFragment = (DownloadListFragment) getFragmentManager().findFragmentById(R.id.download_list);

    bar = getActionBar();
    assert bar != null;

    bar.setDisplayHomeAsUpEnabled(true);
    bar.setDisplayShowTitleEnabled(false);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(ArrayAdapter.createFromResource(this, R.array.download_types,
            android.R.layout.simple_spinner_dropdown_item), this);

    int state = -1;
    String action = getIntent().getAction();
    if (action != null) {
        if (action.equals(FLASH_ROM_ACTION)) {
            state = GOTO_TYPE_ROM;
            showFlashDialog(RomInfo.FACTORY.fromIntent(getIntent()));
            /* } else if (action.equals(FLASH_KERNEL_ACTION)) {
                state = GOTO_TYPE_KERNEL;
                showFlashDialog(KernelInfo.FACTORY.fromIntent(getIntent())); */
        } else {
            state = getIntent().getIntExtra(EXTRA_GOTO_TYPE, state);
        }
    }

    if (savedInstanceState != null) {
        if (state == -1)
            state = savedInstanceState.getInt("state", dlFragment.getState());
    }
    bar.setSelectedNavigationItem(state);
}

From source file:moodle.android.moodle.helpers.FileManager.java

public File DownloadFromUrl(String fileURL, String fileName, String courseDirectoryAndType) { //this is the downloader method

    File file = null;/*from   w  w  w.  jav a2  s.  c  om*/
    try {
        URL url = new URL(fileURL); //you can write here any link

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

        if (mExternalStorageAvailable || mExternalStorageWriteable) {

            // create a File object for the parent directory 
            File fileDirectory = new File(PATH + "/Moodle/" + courseDirectoryAndType);
            // have the object build the directory structure, if needed. 
            fileDirectory.mkdirs();
            // create a File object for the output file 
            file = new File(fileDirectory, fileName);

            long startTime = System.currentTimeMillis();
            Log.d(TAG, "download begining");
            Log.d(TAG, "download url:" + url);
            Log.d(TAG, "downloaded file name:" + fileName);

            /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

            /*
             * Define InputStreams to read from the URLConnection.
             */
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).
             */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            /* Convert the Bytes read to a String. */
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();

            Log.d(TAG, "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
        }

    } catch (IOException e) {
        Log.d(TAG, "Error: " + e);
    }

    return file;
}

From source file:Main.java

public static void copyFolder(AssetManager assetManager, String source, String target) {
    // "Name" is the name of your folder!
    String[] files = null;//ww  w .j  a  va 2s .c om

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        // Checking file on assets subfolder
        try {
            files = assetManager.list(source);
        } catch (IOException e) {
            Log.e("ERROR", "Failed to get asset file list.", e);
        }
        // Analyzing all file on assets subfolder
        for (String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            // First: checking if there is already a target folder
            File folder = new File(target);
            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }
            if (success) {
                // Moving all the files on external SD
                String sourceFile = source + "/" + filename;
                String targetFile = folder.getAbsolutePath() + "/" + filename;
                try {
                    in = assetManager.open(sourceFile);
                    out = new FileOutputStream(targetFile);
                    /*Log.i("WEBVIEW",
                      Environment.getExternalStorageDirectory()
                            + "/yourTargetFolder/" + name + "/"
                            + filename);*/
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                } catch (IOException e) {
                    try {
                        assetManager.list(sourceFile);
                    } catch (IOException f) {
                        Log.e("ERROR", "Failed to copy asset file: " + filename, f);
                        continue;
                    }

                    copyFolder(assetManager, sourceFile, targetFile);
                }
            } else {
                // Do something else on failure
            }
        }
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
    } else {
        // Something else is wrong. It may be one of many other states, but
        // all we need
        // is to know is we can neither read nor write
    }
}

From source file:org.digitalcampus.oppia.utils.storage.ExternalStorageStrategy.java

public boolean isStorageAvailable(Context ctx) {
    String cardStatus = Environment.getExternalStorageState();
    if (cardStatus.equals(Environment.MEDIA_REMOVED) || cardStatus.equals(Environment.MEDIA_UNMOUNTABLE)
            || cardStatus.equals(Environment.MEDIA_UNMOUNTED)
            || cardStatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)
            || cardStatus.equals(Environment.MEDIA_SHARED)) {
        Log.d(TAG, "card status: " + cardStatus);
        return false;
    } else {/*  w  w  w. jav  a  2  s .c  o m*/
        return true;
    }
}