Android Environment.getExternalStorageDirectory()不起作用?

时间:2014-08-12 11:08:38

标签: android

这是我之前的代码

  

private static final String ROOTPATH =“/ data / data /”;

我希望将其更改为使用环境:

  

private static final String ROOTPATH =   Environment.getExternalStorageDirectory()+ “/”;

但我得到了这个结果:

> 08-12 10:53:09.131: E/SQLiteDatabase(1051):
> android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error
> (code 14): Could not open database 08-12 10:53:09.131:
> E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
> 08-12 10:53:09.131: E/SQLiteDatabase(1051):   at
> android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)

这是我的工作:     ........     ........

private static final String ROOTPATH = Environment.getExternalStorageDirectory()+"/";
private static final String DB_NAME_INDBNAME    = "database.jpg";
private static final String DB_NAME_OUTDBNAME   = "database.db";

...... 
public synchronized static NOLDatabaseHelper getInstance(Context context) {
    if(dbHelper == null) {
         dbHelper = new NOLDatabaseHelper(context);
    }

    return dbHelper;
}


private NOLDatabaseHelper(Context context) {
    super(context, DB_NAME_OUTDBNAME, null, DB_VERSION);
    this.context = context;
}


public void createDatabase() {
    if(!isDatabaseExited()) 
    {
        Log.v(GlobalConfig.TAG, "NO database");
        getReadableDatabase();
        copyDataBaseFromAsset();
    } else {
        Log.v(GlobalConfig.TAG, "Has database");
    }
}


private boolean isDatabaseExited() {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase( ROOTPATH + DB_NAME_OUTDBNAME, null,
                SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException e) {}
    return checkDB != null ? true : false;
}


private synchronized void copyDataBaseFromAsset() 
{
    int length                  = -1;
    byte[] buffer               = new byte[1024];
    AssetManager am             = context.getAssets();

    try 
    {
        InputStream assetsDB    = am.open(DB_NAME_INDBNAME);
        String outFileName      = ROOTPATH + DB_NAME_OUTDBNAME;
        OutputStream dbOut      = new FileOutputStream(outFileName);

        while ((length = assetsDB.read(buffer)) > 0) {
            dbOut.write(buffer, 0, length);
        }

        dbOut.flush();
        dbOut.close();
        assetsDB.close();

    } catch(Exception e) {
        Log.e( GlobalConfig.TAG, "copyDataBaseFromAsset error, is say: "+e.toString() );
    }
}
从outSide使用getInstance()调用

createDatabase()。是否有任何错误或遗漏的代码?我该如何解决这个问题?

非常感谢〜!

1 个答案:

答案 0 :(得分:-1)

更改

 super(context, DB_NAME_OUTDBNAME, null, DB_VERSION);

 super(context, ROOTPATH + DB_NAME_OUTDBNAME, null, DB_VERSION);

在您的NOLDatabaseHelper构造函数中,将ROOTPATH更改为

ROOTPATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";