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;
}
Related
String fldr= "Main";
I need to find whether there is a folder called fldr in anywhere in the directory.
This will return the Directory of External storage
String SDpath = Environment.getExternalStorageDirectory().toString();
If there is a folder similar to fldr I need to get its directory and check whether its writable.. How to do it ?
do this way,
File dir = new File(Environment.getExternalStorageDirectory() + "/Main");
if(dir.exists() && dir.isDirectory()) {
// do something here
}
/**
* #return True if the external storage is available.
* False otherwise.
*/
public static boolean checkAvailable() {
// Retrieving the external storage state
String state = Environment.getExternalStorageState();
// Check if available
if (Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
/**
* #return True if the external storage is writable.
* False otherwise.
*/
public static boolean checkWritable() {
// Retrieving the external storage state
String state = Environment.getExternalStorageState();
// Check if writable
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
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!
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);
}
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.
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;
}