12

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();
}
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83

1 Answers1

20

You should compose the path yourself:

String packageName = context.getPackageName();
File externalPath = Environment.getExternalStorageDirectory();
File appFiles = new File(externalPath.getAbsolutePath() +
                         "/Android/data/" + packageName + "/files");
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • you have 2 issues here: 1. it's **Environment.getExternalStorageDirectory()** 2. close the bracket after **+"/files")** – Ahmad Kayyali Jun 07 '11 at 10:21
  • Fixed that. I didn't check this code with compiler. But anyway, this should've shown what I meant. – inazaruk Jun 07 '11 at 10:24
  • @inazaruk: perfect I am testing that right away, I got it before you edited it, my comment was just for the sake of the other people who might face the same issue. – Ahmad Kayyali Jun 07 '11 at 10:29
  • 2
    Thanks :) Also note that on Android 2.1 (and lower) this folder might not get deleted if application is uninstalled. – inazaruk Jun 07 '11 at 10:33
  • @inazaruk: there is a weird behavior I am facing, when i ask about if the File `appFiles` does exist it returns no, do you have any idea? – Ahmad Kayyali Jun 07 '11 at 11:51
  • Do I need to create it my self? – Ahmad Kayyali Jun 07 '11 at 12:00
  • Yes, you need. Use `appFiles.mkdirs()`. – inazaruk Jun 07 '11 at 12:03
  • unfortunately it always returns false flag after executing it. – Ahmad Kayyali Jun 07 '11 at 12:05
  • 1
    If `mkdirs()` function returns false, then the directory already exists. Check it with `appFiled.exists()` or use file browser to verify it was created. – inazaruk Jun 07 '11 at 12:07
  • @inazaruk: Kindly check my updated answer, all I want to do is to get the path where the download file is going to be stored, I know I have been a pain to you, I Am really appreciated. – Ahmad Kayyali Jun 07 '11 at 12:21
  • Hi inazaruk.. This post looks similar to my problem. In android 2.1 updated i can't store image in specified path.. I am really weird.. I hope you will solve my problem and help me to get to next stage... If you can please see my [post.](http://stackoverflow.com/questions/11079515/camera-image-stored-in-dcim-but-not-in-the-specified-folder).thanks in advance.. – vinothp Jun 18 '12 at 10:13