has Mounted Storage - Android Hardware

Android examples for Hardware:Storage

Description

has Mounted Storage

Demo Code


//package com.java2s;
import java.io.File;
import java.io.IOException;

import android.os.Environment;

public class Main {
    static public boolean hasStorage(boolean requireWriteAccess) {

        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return !requireWriteAccess || checkFsWritable();
        } else if (!requireWriteAccess
                && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

            return true;
        }//from ww w.j a  va  2 s  .co m
        return false;
    }

    static private boolean checkFsWritable() {
        String directoryName = Environment.getExternalStorageDirectory()
                .toString() + "/dcim";

        File directory = new File(directoryName);

        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                return false;
            }
        }

        File f = new File(directoryName, ".probe");
        try {
            // Remove stale file if any
            if (f.exists()) {
                f.delete();
            }
            if (!f.createNewFile())
                return false;
            f.delete();
            return true;
        } catch (IOException ex) {
            return false;
        }
    }
}

Related Tutorials