i am creating Download Manager app therefore my requirement is to store files on sdcard and phone internal storage if sdcard not available and access them later to open files from app.
i have read almost every post to store files but it confuses me to decide which method is best to use because there are lot of different ways to do.i want to create folder inside external(if available) or internal then store files inside this folder.
Now here can anyone tell me what is best way to access sdcard if available otherwise internal storage
Storage options in android is the place where you need to go. You will know how to Checking media availability from there, more specifically external storage.
The sample code is
/* 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;
}
Use Environment to access ExternalStorageDirectory
Related
In my app, im trying to check whether the user has connected their phone to the pc as a drive, so I can warn them to disconnect it because I need access to the storage.
It works fine on all 4-5 devices that I've tested on, except this HTC Desire X phone, Android 4.0.4 . It has no SD card, but there is about 2 gb of some storage available for writing.
Here is the code that I use
private void checkStorage() {
// Get the external storage's state
String state = Environment.getExternalStorageState();
Log.d("STATE", state);
if (state.equals(Environment.MEDIA_MOUNTED)) {
// Storage is available and writeable
externalStorageAvailable = externalStorageWriteable = true;
} else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
// Storage is only readable
externalStorageAvailable = true;
externalStorageWriteable = false;
} else {
// Storage is neither readable nor writeable
externalStorageAvailable = externalStorageWriteable = false;
}
}
So when I run it, the logcat debug tag returns:
removed
According to documentation:
http://developer.android.com/reference/android/os/Environment.html#MEDIA_REMOVED
"Storage state if the media is not present."
How can I modify this code so it detects the presence of a memory that is available for use?
Now, there is another thing. On this phone, when I try downloading an image from the browser, Im not allowed to, and I get the following message:
No SD card
An SD card is required to download <filename>
OK
Is there something wrong with the phone or is this normal? How do I make my app work on this phone?
EDIT: Furthermore, if I am able to get past this, how do I write files to the storage? Here's my code that does that and works on other devices:
File directory = null;
File file = null;
try {
directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
Utils.getWritableDiectory(Locale.ENGLISH));
if (!directory.exists()) {
directory.mkdirs();
}
file = new File(directory, "data.json");
Some android devices has built-in flash memory, and we can check this memory at Environment.getExternalStorageDirectory(). But, when we insert an SD card, then the system differentiates these two with /storage/sdcard0 and /storage/sdcard1.
I am not sure whether /storage/sdcard0 is flash memory or SD card memory. Could anyone provide any explanation as to which is which?
Use the method Environment.isExternalStorageRemovable() to determine if it's a removable SD card or not.
Edit for clarification:
Consider the following method:
public enum ExternalStorageStatus {
READ_WRITE , READ_ONLY, NONE
}
public static ExternalStorageStatus checkExternalStorageStateAvailable(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return ExternalStorageStatus.READ_WRITE;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return ExternalStorageStatus.READ_ONLY;
} else {
return ExternalStorageStatus.NONE;
}
}
The method Environment.getExternalStorageState() returns the current state of your storage. If no REAL external storage (i.e. a medium in which isExternalStorageRemoveable() == false), then this returns the current state of your internal storage. If isExternalStorageRemoveable() == true, then getExternalStorageState() returns the state of your removable SD card.
Actual external storage is prioritized.
I am facing a problem while trying to write a files into External SD Card in android 4.0.3,even I have made use of the write permissions like WRITE_EXTERNAL_STORAGE & WRITE_MEDIA_STORAGE, So I have found another way to work on it, but now I want to find out whether my app is possible to write into the Removable SD Card
or not, if not then I would like to switch to Internal SD Card.
So now the question is how will I come to know whether writing into Removable SD Card supported or not, So if anyone has done any similar kind of an implementation please feel free to share the code here.
Answer directly from the API guide:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
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;
}
I have an app that writes to both the internal and external SD cards. I have the WRITE_EXTERNAL_STORAGE permission, but nothing else.
The main problem in dealing with the external SD card is to find the path, because getExternalStorageDirectory() usually returns the internal one. It should be a subdirectory of /mnt, if that helps.
To find if you can write to SD card take a look at the Environment class.
String externalStorageState = Environment.getExternalStorageState();
you can then check this state using
Environment.MEDIA_MOUNTED(externalStorageState) and/or Environment.MEDIA_MOUNTED_READ_ONLY(externalStorageState)
I have an Android app which by preference uses external storage if available to store various files but will use internal storage if external storage is unavailable.
I've extended Application and maintain a static File for the app's working directory as follows...
public class MyApp extends Application {
protected static File myFilesDir = null;
protected static Helper myHelper = null;
#Override
public void onCreate() {
super.onCreate();
myHelper = new Helper(this);
if (myHelper.CanWriteExtStorage()) {
Log.d(TAG, "onCreate() - myHelper.CanWriteExtStorage() returned TRUE");
myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + packageName + "/files");
}
else {
Log.d(TAG, "onCreate() - myHelper.CanWriteExtStorage() returned FALSE");
myFilesDir = new File(getFilesDir().getAbsolutePath());
}
myFilesDir.mkdirs();
if (!myFilesDir.equals(null)) {
Log.d(TAG, "onCreate() - myFilesDir: " + myFilesDir.getAbsolutePath());
}
The app is currently being beta-tested by some people I'm in direct contact with and one guy commented that he thought I was supposed to be using the SD card if available but he could see from logcat the app was using /nand/Android/data... I asked what /nand referenced and he said it is internal memory on his pad/tablet device.
The code the Helper class uses to check for external storage is as follows...
protected boolean CanWriteExtStorage() {
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
mExternalStorageWriteable = true;
return mExternalStorageWriteable;
}
...but the relevant log output is as follows...
D/com.mycompany.myapp(3844): onCreate() - myHelper.CanWriteExtStorage() returned TRUE
D/com.mycompant.myapp(3844): onCreate() - myFilesDir: /nand/Android/data/com.company.myapp/files
So, the question is why, when I'm able to test that the external storage state shows it is mounted, would Environment.getExternalStorageDirectory() return a path to what appears to be internal storage?
Should I be testing for something other than Environment.MEDIA_MOUNTED or is there just something strange about this device or Android version (he is running v2.1).
In the long run it possible doesn't matter but I'm concerned my logic is incorrect for certain devices.
Read the documentation for Environment.getExternalStorageDirectory()
Note: don't be confused by the word
"external" here. This directory can
better be thought as media/shared
storage. It is a filesystem that can
hold a relatively large amount of data
and that is shared across all
applications (does not enforce
permissions). Traditionally this is an
SD card, but it may also be
implemented as built-in storage in a
device that is distinct from the
protected internal storage and can be
mounted as a filesystem on a computer.
In devices with multiple "external"
storage directories (such as both
secure app storage and mountable
shared storage), this directory
represents the "primary" external
storage that the user will interact
with.
I have not personally seen a case but there is no requirement that getExternalStorageDirectory returns the memory card. You are doing the correct thing by treating the returned directory as shared storage as it was an intentional decision by the device manufacturer.
Using the api logic you can detect if the sd card is available for read or write, but it doesn't tell you why it is not writable.
I want to know if the user even has a sd card vs if it just mounted.
Is this possible?
for example:
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
}
Check out the possible constants at : http://developer.android.com/reference/android/os/Environment.html#getExternalStorageState%28%29
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}