返回String路径以供以后使用

时间:2014-06-19 10:14:06

标签: android file android-external-storage

在我的应用程序中,我将不断使用我的存储的DirectoryPath。在我的MAIN中,我最初创建了一个目录,我想将它存储在一个字符串中,以便在其他方法中使用,例如当我创建实际数据库时,我希望它在此路径中创建。我已经把自己扭曲了每个人的建议,而是返回一个布尔值。我不确定是否会使用该值。

这就是我所拥有的:

    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;
}

另一个问题是,这实际上并没有像我想要的那样在SDCARD中创建目录。它在/ storage / emulated / 0 / APP /数据库中创建目录,我假设它是可能被指定为外部的内部存储。如果这个应用程序开始创建一个100mb或更多的数据库并且他们没有空间,这将是一个问题。如何将其送到SDCARD?

请忽略我的百万评论。我正在调试NullPointer错误,需要找出具体问题的哪一行。我稍后会删除它们。

1 个答案:

答案 0 :(得分:0)

您是使用模拟器还是手机测试过的?在这两种情况下,是否安装了SD卡? 也许这有助Does Environment.getExternalStorageDirectory() returns a usable data folder when no sd card is mounted?,我认为您需要使用getExternalFilesDir。从JellyBean和多用户环境开始,行为发生了变化,如文档中所述:developer.android.com/about/versions/android-4.2.html。要确保已安装外部SD卡,请使用:getExternalStorageState

下面是一个小片段,将FilePaths返回到String并在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);
}

请注意getExternalStoragePublicDirectory,并确保使用正确的常量,在此处记录:http://developer.android.com/reference/android/os/Environment.html段落字段。