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

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

Maybe you should change this:
if (!Environment.MEDIA_MOUNTED.equals(state)){
into this:
if (Environment.MEDIA_MOUNTED.equals(state)){
(ie: remove the "!")

Related

Cannot find file/dir written to SD-card

I need help on a tricky issue. I write an app that shall work with a file being provided on the SD card - manipulates this file - and writes it back to the SD card. So far so good. Everything is working fine within the Eclipse environment with SD emulation. As soon as I install the app on my Sony Xperia - the app does not find the file on the SD card - nor can I find files/dirs which I store/create on the SD card.
I am issuing the test-app I wrote for the Sony and kindly ask you for help on this issue!
Here is the code snippet of the app - trying to create a directory:
public class IOTestActivity extends ActionBarActivity {
private boolean mExternalStorageAvailable;
private boolean mExternalStorageWriteable;
TextView debugInfo;
TextView availInfo;
TextView writeInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iotest);
debugInfo = (TextView)findViewById(R.id.iotestLbLDebugInfo);
checkSDCard();
fillView();
readSDCard();
}
private void readSDCard() {
debugInfo.append("...start reading SD dirs\n\n");
if ((mExternalStorageAvailable==true) &&
(mExternalStorageWriteable == true)) {
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
debugInfo.append("SD-Root='" + root + "'\n");
File newDir = new File (root, "TEST");
boolean result = newDir.mkdir();
debugInfo.append("...creating new dir '" + newDir + "' = " + result);
}
}
private void fillView() {
debugInfo.setText("...entering IO Test\n");
debugInfo.append("...reading SD-card availability\n");
availInfo = (TextView)findViewById(R.id.iotestLblInfo1);
availInfo.setText("" + mExternalStorageAvailable);
debugInfo.append("...reading SD-card writing rights\n");
writeInfo = (TextView)findViewById(R.id.iotestLblInfo2);
writeInfo.setText("" + mExternalStorageWriteable);
}
private void checkSDCard() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
}
Of course I have added to my Manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
Here ist the test-output of the app:
BTW: the problem exists independent from the fact if I use:
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
or
File root = Environment.getExternalStorageDirectory();
And when I access the SD-card via USB-connected phone through the file explorer, I always receive the same contents of the SD card:
Thanks for help!

Android ,No backup to the extern sd card (/storage/sdcard1/)

I m trying to save a file in sd card at the following path.
Environment.getExternalStorageDirectory().getAbsolutePath() + dir + filename
if sd card state is mounted.
String state = Environment.getExternalStorageState();
One of my user complained
No backup to the extern sd card (/storage/sdcard1/)
This issue raied in Galaxy Tab2 10.1.
I am little confused,i have nexus 4,where it has only internal memory.
and verified the same in S4,s3 and note 3 all worked fine.
Hope i am saving the file in right path and works fine in all devices.
how do i resolve complain ? any things is wrong ?
.
The SD card might not be ready yet. Consider waiting for external storage to get ready, before writing to it, as shown below.
// Wait till external storage is initialized upon startup.
private void awaitExternalStorageInitialization() {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
int count = 0;
do {
String state = Environment.getExternalStorageState();
if(count > 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Logger.log(e.getMessage(), Logger.LogType.ERROR, e);
}
}
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;
}
count++;
} while ((!mExternalStorageAvailable) && (!mExternalStorageWriteable)
&& (count < 15));
if(!mExternalStorageWriteable)
Logger.log("External storage not ready yet", Logger.LogType.ERROR, null);
}

Different State of ExternalStorageState

Explain in which situation (ex:phone connected as media to computer, SD card unmounted), below conditions will be met.
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
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;
}
This will check whether the external storage is available to read and write. The getExternalStorageState() method returns other states that you might want to check, such as whether the media is being shared (connected to a computer), is missing entirely, has been removed badly, etc. You can use these to notify the user with more information when your application needs to access the media.
Environment.MEDIA_MOUNTED
Check Storage state if the media is present and mounted at its mount point with read/write access.
It will be true if the SD card is available.
Environment.MEDIA_MOUNTED_READ_ONLY
It will true if sdcard is available and its Read only.

how to check in code whether an external storage device is read only

In my android app I write some files to SD card.
However there is a condition that if the SD card is READ-ONLY
then I have to change screen settings accordingly.
How can I check it in my code if a SD card is read only ?
try using Environment.MEDIA_MOUNTED_READ_ONLY as:
String status = Environment.getExternalStorageState();
if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
Toast.makeText(Activity.this, "SD MEDIA_MOUNTED", Toast.LENGTH_LONG).show();
} else if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED_READ_ONLY)) {
Toast.makeText(Activity.this, "MEDIA_MOUNTED_READ_ONLY",
Toast.LENGTH_LONG).show();
}
And more full answer...
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;
// check_folder();
} 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;
}

Why am I getting "Parent directory of file is not writable?"

I've reviewed numerous posts but still have no answer. After adding WRITE_EXTERNAL_STORAGE then checking that the sd card is writable and mounted, I still get "Parent directory of file is not writable." I'm using eclipse and the emulator and targeting SdkVersion="7".
How can I write to the sd card?
Example code follows:
<uses-premission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
.
.
.
checkSDStatus();
.
.
.
File sdCardRoot = Environment.getExternalStorageDirectory();
// Check state of SD card
String sdState = Environment.getExternalStorageState();
Log.d("--- state ---", sdState);
File dir = new File (sdCardRoot.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, fileName);
.
.
.
private void checkSDStatus(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.d("-- sd card --", "-- card writable --");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.d("-- sd card --", "-- card read only --");
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.d("-- sd card --", "-- card unvailable --");
}
}
Okay, I'll answer my question for others.
After adding to my manifest file several times previously I had nothing to lose so I copied another version of it and added it to the manifest below the existing permission.
The permissions appeared identical to the eye but I deleted my old one and used the new I had copied in.
I reran the app and it worked!!! Perhaps there was a bug on the disk, the manifest file wasn't getting read for some reason, or whatever - at least it now works!
If anyone knows why I'd sure like to hear the reason for this problem!

Categories

Resources