javaandroidandroid-2.1-eclair

getExternalFilesDir alternative in android 2.1


I built an android app on android 2.2, for saving files into the SD card I use the following:

 context.getExternalFilesDir(null).getAbsolutePath();

returning a string like:

 /mnt/sdcard/Android/data/com.hello.example1/files

Now I need to make my app compatible with android 2.1, what method do I need to use to get the external files directory?


public static String sTellMeWhereToSaveMyData(Context context) 
{
        String packageName = context.getPackageName();
        File externalPath = Environment.getExternalStorageDirectory();
        File appFiles = new File(externalPath.getAbsolutePath() + "/Android/data/" + packageName+ "/");

        if (appFiles.exists() && appFiles.isDirectory()) 
        {
            return appFiles.getAbsolutePath();
        }
        else 
        {
            if(appFiles.exists())
            {
                Log.v("File Manager","not exists");
            }
            if (!appFiles.mkdir())
            {
                Log.v("File Manager","Could not create");
            }   
        }
        return appFiles.getAbsolutePath();
}

Solution

  • You should compose the path yourself:

    String packageName = context.getPackageName();
    File externalPath = Environment.getExternalStorageDirectory();
    File appFiles = new File(externalPath.getAbsolutePath() +
                             "/Android/data/" + packageName + "/files");