Mega Code Archive

 
Categories / Android / Core Class
 

Is Storage ReadableWritable

//package ch.uzh.ifi.attempto.mobileape; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.os.Environment; import android.widget.Toast; public class Utils {   public static boolean isStorageWritable() {     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;     }     //Toast.makeText(TransAnd.this, "SD: available: " + mExternalStorageAvailable + "; writable: " + mExternalStorageWriteable, Toast.LENGTH_LONG).show();     return (mExternalStorageAvailable && mExternalStorageWriteable);   }   public static boolean isStorageReadable() {     boolean mExternalStorageAvailable = false;     String state = Environment.getExternalStorageState();     if (Environment.MEDIA_MOUNTED.equals(state)) {       mExternalStorageAvailable = true;     } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {       mExternalStorageAvailable = true;     }     return mExternalStorageAvailable;   }   /**    *     * @param filePath    * @return    * @throws java.io.IOException    */   public static String readFileAsString(String filePath) throws java.io.IOException {     byte[] buffer = new byte[(int) new File(filePath).length()];     BufferedInputStream f = null;     try {       f = new BufferedInputStream(new FileInputStream(filePath));       f.read(buffer);     } finally {       if (f != null) try { f.close(); } catch (IOException ignored) { }     }     return new String(buffer);   }   public static List<String> readFileToLines(String filePath) throws IOException {     List<String> lines = new ArrayList<String>();     FileInputStream fstream = new FileInputStream(filePath);     DataInputStream in = new DataInputStream(fstream);     BufferedReader br = new BufferedReader(new InputStreamReader(in));     String strLine;     while ((strLine = br.readLine()) != null)   {       lines.add(strLine);     }     in.close();     return lines;   }   public static void showMessage(Context context, String message) {     Toast.makeText(context, message, Toast.LENGTH_LONG).show();   }   public static void showException(Context context, Exception e) {     Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();   } }