getExternalFilesDir alternative in android 2.1
Asked Answered
W

1

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();
}
Warrantor answered 7/6, 2011 at 10:3 Comment(0)
J
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");
Joline answered 7/6, 2011 at 10:16 Comment(11)
you have 2 issues here: 1. it's Environment.getExternalStorageDirectory() 2. close the bracket after +"/files")Warrantor
Fixed that. I didn't check this code with compiler. But anyway, this should've shown what I meant.Joline
@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.Warrantor
Thanks :) Also note that on Android 2.1 (and lower) this folder might not get deleted if application is uninstalled.Joline
@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?Warrantor
Do I need to create it my self?Warrantor
Yes, you need. Use appFiles.mkdirs().Joline
unfortunately it always returns false flag after executing it.Warrantor
If mkdirs() function returns false, then the directory already exists. Check it with appFiled.exists() or use file browser to verify it was created.Joline
@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.Warrantor
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..thanks in advance..Sebaceous

© 2022 - 2024 — McMap. All rights reserved.