Hank Hank - 1 month ago 11
Android Question

getExternalStorageState() showing 'mounted' but still unable to create folder

Manifest has

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">


Application settings, 'Storage' = modify/delete SD card contents'
Same results on Samsung Tablet running 2.3.5 and Motorola Droid running 2.3.4.
Devices are not tethered to development machine.

Code follows:

public class OutputStudentRecords extends StActivity{
SharedPreferences mStudentSettings;
protected Cursor mCursor;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_csv);

String state = Environment.getExternalStorageState();
Toast.makeText(getApplicationContext(),"State is " + state, Toast.LENGTH_LONG).show();

if (!Environment.MEDIA_MOUNTED.equals(state)){
//We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
Toast.makeText(getApplicationContext(), "We Can Read And Write ", Toast.LENGTH_LONG).show();
File file = new File(Environment.getExternalStorageDirectory()
+File.separator
+"studentrecords"); //folder name
file.mkdir();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Toast.makeText(getApplicationContext(), "We Can Read but Not Write ", Toast.LENGTH_LONG).show();
}else{
//something else is wrong
mExternalStorageAvailable = mExternalStorageWriteable = false;
Toast.makeText(getApplicationContext(), "We Can't Read OR Write ", Toast.LENGTH_LONG).show();
}

}

}


Toast returns State="mounted" however it skips down to "we can't read or write" on both machines. I've missed something but can't find it, any help would be appreciated.

Thank you

Answer

Maybe you should change this:

if (!Environment.MEDIA_MOUNTED.equals(state)){

into this:

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

(ie: remove the "!")