The below code shows me that the MediaScanner has started on the sdcard, after it ejects why? and what is happening?
if(intent.getDataString().equals("file:///mnt/extsd"))
{
if(Intent.ACTION_MEDIA_SCANNER_STARTED.equals(intent.getAction()))
{
//Media scanner is started
}
else if(Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(intent.getAction()))
{
}
}
Probably this question will be helpfull Its about /mnt/extsd and how to use SD card properly (means Environment.getExternalStorageDirectory()). Also you have to check the SD card state. I mean like:
boolean isExternalStorageWriteable = false, isExternalStorageReadable = false;
// Check SD Card for Read/Write
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
isExternalStorageWriteable = true;
isExternalStorageReadable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
isExternalStorageReadable = true;
} 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
}
Related
I have a device with an SD card. Now I want to check that device has mounted an external SD card and can read files from the public DCIM folder. I know that I can use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);, but this method returns only files, that is on primary external memory, not on the mounted SD card (Secondary external storage).
I found that Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); returns an element with index 1, for the secondary external storage, but this method gets files only for application sandbox (Android/data/packagename). So my question is how get path to secondary external path for public directory like DCIM?
I found solution, here is code snippet:
String strSDCardPath = System.getenv("SECONDARY_STORAGE");
if ((strSDCardPath == null) || (strSDCardPath.length() == 0)) {
strSDCardPath = System.getenv("EXTERNAL_SDCARD_STORAGE");
}
//If may get a full path that is not the right one, even if we don't have the SD Card there.
//We just need the "/mnt/extSdCard/" i.e and check if it's writable
if(strSDCardPath != null) {
if (strSDCardPath.contains(":")) {
strSDCardPath = strSDCardPath.substring(0,strSDCardPath.indexOf(":"));
}
File externalFilePath = new File(strSDCardPath);
if (externalFilePath.exists() && externalFilePath.canWrite()) {
//do what you need here
}
}
For more details, read here: Finding the SDCard Path on Android devices
Have you tried this?
private boolean canWriteToFlash() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Read only isn't good enough
return false;
} else {
return false;
} }
For accessing multiple external storage, you could use the api
ContextCompat.getExternalCacheDirs(Context context);
ContextCompat.getExternalFilesDirs(Context context, String type);
This will return a path like /storage/sdcard1/Android/data/com.example.foo/, then you can replace Android/data/com.example.foo/ with DCIM to get the path you want.
This method is not reliable, because the path Android/data/com.example.foo/ will be different or changed in the future, but it worths to have a try.
You can see more information from the official android documents.
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);
}
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.
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;
}
My app will require external storage (SD card) in order to save the user's data. I've searched high and low, but I can't find any best practices for this.
Do I...
Simply assume external storage will be present and skip adding checks and alerts? (Assumption here would be that external storage only goes missing in rare cases, and it's not worth spending a ton of time coding a solution for this. Typical users don't ever remove their SD cards, etc.)
Check that external storage is mounted only when reading from or writing to it? (Users may remove their SD card at any time, but the app will still work until it comes time to access their data.)
Add a listener at the application level that waits for external storage notifications and displays a modal alert app-wide? (This sounds ideal, but could also be overkill for all I know.)
Any insight will be greatly appreciated.
Yes u have to test for the presence of the sdcard the following function would help you.
private boolean checkForDirectory()
{
boolean cardstate = true;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_BAD_REMOVAL.equals(state)) {
cardstate = false;
runDialog("Memory Card was removed before it was unmounted");
}
else if (Environment.MEDIA_CHECKING.equals(state)) {
runDialog("Memory Card is present and being disk-checked");
}
else if (Environment.MEDIA_MOUNTED.equals(state)) {
cardstate = true;
//runDialog("Memory Card is present and mounted with read/write access");
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
runDialog("Memory Card is present and mounted with readonly access");
}
else if (Environment.MEDIA_NOFS.equals(state)) {
cardstate = false;
runDialog("Memory Card is present but is blank or using unsupported file system");
}
else if (Environment.MEDIA_REMOVED.equals(state)) {
cardstate = false;
runDialog("Memory Card is not present");
}
else if (Environment.MEDIA_SHARED.equals(state)) {
cardstate = false;
runDialog("Memory Card is present but shared via USB mass storage");
}
else if (Environment.MEDIA_UNMOUNTABLE.equals(state)) {
cardstate = false;
runDialog("Memory Card is present but cannot be mounted");
}
else if (Environment.MEDIA_UNMOUNTED.equals(state)) {
cardstate = false;
runDialog("Memory Card is present but not mounted");
}
File dir = new File(Environment.getExternalStorageDirectory() + "/gwc");
if(dir.exists() && dir.isDirectory())
{
System.out.println("Folder exists");
}
else
{
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + "/gwc");
myNewFolder.mkdir();
System.out.println("Folder gwc created ");
}
return cardstate;
}
You should definitely check for it before using it. I would suggest #2; you're right, #3 does seem like overkill (I don't even know if there is a listener for that). The Google docs has this to say:
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state.
Absolutely agree with Shawn Lauzon's answer, and here is a post on developer.android.com, that has some code for checking if external storage is available and if it's writable. Hope this helps you.
Data Storage