Different State of ExternalStorageState - android

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.

Related

Detect removable external storage in Android

As I know, there are 3 kinds of storage,
Internal Storage: Its private and no one can access to it. For
example:File internalFile = new File(getFilesDir(), "MyFile.txt");
External Storage: It is not removable.For
example: File externalFile = new File(Environment.getExternalStorageDirectory(), "MyFile.txt");
Secondary External Storage: It can be mounted or unmounted by the user.
My question is for third type of storage. How can I access to secondary SD card? I have searched for hours but I did not find the answer.
To detect sd card availability you can do it on this way
boolean mExtStorage = false;
boolean mExtWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExtStorage = mExtWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExtStorage = true;
mExtWriteable = false;
} else {
mExtStorage = mExtWriteable = false;
}
There are only 2 types,
Internal Storage
External storage, which can be removable (SD card) or non-removable.
Check this guide for more details. I hope it has all that you need.
You can detect the storage state, as specified in the guide, using the following code
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}

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);
}

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
Maybe you should change this:
if (!Environment.MEDIA_MOUNTED.equals(state)){
into this:
if (Environment.MEDIA_MOUNTED.equals(state)){
(ie: remove the "!")

Scanning different memory types in android

I have some queries which may be useful to others too.
In android 4.0 onwards ,
1)how to check whether external sd card support is there or not ?
2)How to run Mediascan forcefully both internal and external memory ?
3)How to Mediascan only sd card or internal memory?
I'd recommend reading storage options on developer.android.com.
To check external memory is available (taken from developer.android.com):
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;
}
To read internal files use context.fileList(); see more here.
Edit
I'm not sure what you want with 2 and 3. You can use mediascan to many things but using it just for using it sounds unproductive. For that I'd recommend #Singularity advice. There is a post here about using mediascan for pdfs.

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

Categories

Resources