Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 733
Ahmad Kayyali Avatar asked Jun 07 '11 10:06

Ahmad Kayyali


1 Answers

You should compose the path yourself:

String packageName = context.getPackageName();
File externalPath = Environment.getExternalStorageDirectory();
File appFiles = new File(externalPath.getAbsolutePath() +
                         "/Android/data/" + packageName + "/files");
like image 178
inazaruk Avatar answered Nov 16 '22 12:11

inazaruk