Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed an issue related to retrieving the external storage location.
Normally you would use the method "Environment.getExternalStorageDirectory()" to find the location to the SD card. However on some phones, this returns the wrong location (it will probably return /mnt/sdcard), most likely it will point to the internal storage. On some phones, the SD card is actually mounted on /mnt/sdcard-ext (Motorola) or /mnt/sdcard/external_sd (Samsung). This commit solves the issue by manually checking the possible storage locations (as reported on Stackoverflow) and will return this location if it is writable. If none of the predefined storage locations exist (or arent writable) it will return the default path by using "Environment.getExternalStorageDirectory()"

More information about this Android related issue can be found here:
- http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location
- http://stackoverflow.com/questions/5524105/how-could-i-get-the-correct-external-storage-on-samsung-and-all-other-devices
- http://beyondpod.com/support/index.php?/Knowledgebase/Article/View/30/0/how-to-change-the-location-of-downloaded-podcasts
  • Loading branch information
falcon4ever committed Feb 19, 2012
1 parent d3ddf8e commit 7a9cf0b
Showing 1 changed file with 61 additions and 1 deletion.
Expand Up @@ -37,6 +37,65 @@

public class OFAndroid {

static private String tryExternalStorageDirectory(final String path)
{
File SDCardDir = new File(path);
if(SDCardDir.exists())
{
if(SDCardDir.canWrite())
{
Log.d("OF", "Found writable location: " + path);
return path;
}
}

return "false";
}

static public String getRealExternalStorageDirectory()
{
// Check special cases (Priority top to bottom)
String case1 = tryExternalStorageDirectory("/mnt/sdcard-ext");
String case2 = tryExternalStorageDirectory("/mnt/sdcard/external_sd");
String case3 = tryExternalStorageDirectory("/sdcard/sd");
String case4 = tryExternalStorageDirectory("/mnt/external_sd");
String case5 = tryExternalStorageDirectory("/emmc");
String case6 = tryExternalStorageDirectory("/mnt/sdcard/bpemmctest");
String case7 = tryExternalStorageDirectory("/mnt/sdcard/_ExternalSD");
String case8 = tryExternalStorageDirectory("/mnt/Removable/MicroSD");
String case9 = tryExternalStorageDirectory("/Removable/MicroSD");
String case10 = tryExternalStorageDirectory("/mnt/external1");

String externalPath = "";

if(!case1.equals("false"))
externalPath = case1;
else if(!case2.equals("false"))
externalPath = case2;
else if(!case3.equals("false"))
externalPath = case3;
else if(!case4.equals("false"))
externalPath = case4;
else if(!case5.equals("false"))
externalPath = case5;
else if(!case6.equals("false"))
externalPath = case6;
else if(!case7.equals("false"))
externalPath = case7;
else if(!case8.equals("false"))
externalPath = case8;
else if(!case9.equals("false"))
externalPath = case9;
else if(!case10.equals("false"))
externalPath = case10;
else
externalPath = Environment.getExternalStorageDirectory().getAbsolutePath(); // If none of the locations exist, use default method.

Log.d("OF", "ExternalStorageDirectory: " + externalPath);

return externalPath;
}

public OFAndroid(String packageName, Activity ofActivity){
//Log.i("OF","external files dir: "+ ofActivity.getApplicationContext().getExternalFilesDir(null));
OFAndroid.packageName = packageName;
Expand All @@ -53,7 +112,8 @@ public OFAndroid(String packageName, Activity ofActivity){

dataPath="";
try{
dataPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//dataPath = Environment.getExternalStorageDirectory().getAbsolutePath();
dataPath = getRealExternalStorageDirectory();
dataPath += "/"+packageName;
Log.i("OF","creating app directory: " + dataPath);
try{
Expand Down

0 comments on commit 7a9cf0b

Please sign in to comment.