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()
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.