0

In my application I will be constantly using the DirectoryPath of my storage. In my MAIN I am initially creating the directory I would like to store this in a String to use in other methods such as when I create the actual database, I want it to be created in this path. I have gotten myself twisted up with everyone's recommendations and am returning a boolean value instead. Not sure I will use that value at all.

Here is what I have:

    private static boolean mCreateDatabaseDirectory(String dbPath) {
    boolean directoryCreated = true;
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;


    Log.d("GENERAL", "Moving to mExternalStorageAvailable");
    mExternalStorageAvailable();
    Log.d("GENERAL", "Returning values of Available and Writeable");
    if(mExternalStorageAvailable = mExternalStorageWriteable = true){
        Log.d("GENERAL", "Booleans are True, creating Directoy");

        File directoryPath = new File(Environment.getExternalStorageDirectory().toString()+"/APP/database");
        Log.d("GENERAL", "directoryPath = " + directoryPath);

        if(!directoryPath.exists()) {
            directoryPath.mkdirs();
            String databasePath = directoryPath.toString();
            Log.d(DATABASE, "Database directory path created as " + databasePath);

                //If we try to create the file but there is a problem then Log the error
                if(!directoryPath.mkdirs()){
                    Log.e("DATABASE", "Problem creating Database Directory");
                    directoryCreated = false;   
                }

                Log.d("GENERAL", "Directory did not exist but was created");
                return directoryCreated;

        }else {
        Log.d("GENERAL", "Directory already exists, nothing to do here");
        return directoryCreated;    
     }
    }
    return directoryCreated;
}

Another problem is that this does not actually create the directory in the SDCARD as I want it. It creates the directory in the /storage/emulated/0/APP/database which I assume is the internal storage likely designated as external. Well that is going to be a problem if this app starts creating a database that is 100mb or more and they do not have the space. How do I get it to the SDCARD?

Please ignore my million comments. I was debugging a NullPointer Error and needed to find out which line specifically was the problem. I will erase them later.

4

1 回答 1

0

Did you test this with an emulator or your phone? In both cases, is a sdcard mounted? Maybe this helps Does Environment.getExternalStorageDirectory() returns a usable data folder when no sd card is mounted? and I think you need to usegetExternalFilesDir. As of JellyBean and multiuser environments the behaviour changed as stated in the docs: developer.android.com/about/versions/android-4.2.html. To be sure that the external sdcard is mounted use: getExternalStorageState

heres a little snippet converting the FilePaths returned into a String and outputting them on Logcat

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String getDataDirectory = Environment.getDataDirectory().toString();
    String getExternalStorageState = Environment.getExternalStorageState()
            .toString();
    String getExternalStorageDirectory = Environment
            .getExternalStorageDirectory().toString();
    String getExternalStoragePublicDirectory = Environment
            .getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOCUMENTS).toString();
    String getExternalFilesDir = getExternalFilesDir("Documents")
            .toString();

    Log.i("INFO", "getDataDirectory " + getDataDirectory);
    Log.i("INFO", "getExternalStorageState " + getExternalStorageState);
    Log.i("INFO", "getExternalStorageDirectory "
            + getExternalStorageDirectory);
    Log.i("INFO", "getExternalStoragePublicDirectory Documents "
            + getExternalStoragePublicDirectory);
    Log.i("INFO", "getExternalFilesDir Documents " + getExternalFilesDir);

    setContentView(R.layout.activity_main);
}

Be careful about getExternalStoragePublicDirectory and make sure you are using the right constant, documented here: http://developer.android.com/reference/android/os/Environment.html paragraph fields.

于 2014-06-19T10:51:57.527 回答