2014-03-13 30 views
0

我有一個代碼與SD卡中的數據庫一起工作,它在Android 2.3設備中工作正常,但不能在Android 4設備中工作,在Android 2.3中創建數據庫並且或者在SD卡上的路徑上打開,並且在android 4中,數據庫在手機中的應用程序的默認datadir中創建或打開。下面的代碼...Sqlite打開助手在Android 2.3中工作正常,但不工作在Android 4中

public class dBeeHelper extends SQLiteOpenHelper { 
public final static String DATABASE_NAME = "data.db"; 
private final static int DATABASE_VERSION = 59; 
public static final String DATABASE_FILE_PATH = Environment.getExternalStorageDirectory().toString(); 
public static tables db = new tables(); 
private Context mcontext; 
    public dBeeHelper(Context context) { 
    super(new ContextWrapper(context) { 
      public SQLiteDatabase openOrCreateDatabase(String name, 
       int mode, SQLiteDatabase.CursorFactory factory) { 

       // allow database directory to be specified 
       File dir = new File(DATABASE_FILE_PATH + "/Manager/"); 
       if(!dir.exists()) { 
        dir.mkdirs(); 
       } 
       Manager manager = ((Manager)getApplicationContext()); 
       if (dir.canWrite()){ 
        manager.setDbLocation(dataFiles.Location.SdCard); 
       }else{ 
        manager.setDbLocation(dataFiles.Location.Phone); 
       } 
return SQLiteDatabase.openDatabase(DATABASE_FILE_PATH + "/Manager/" + DATABASE_NAME, null, 
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY); 
      } 
     }, DATABASE_NAME, null, DATABASE_VERSION); 
     this.mcontext = context; 
} 

當我調試應用程序,在安卓2.3 openOrCreateDatabase被激發,在安卓4.0它不被解僱了!

謝謝!

回答

0

問題解決了!我更改了構造函數,它也在android 4中工作。 下面的代碼已更改。

private static Manager manager; 
public dBeeHelper(Context context) { 
     super(context, getpath(context) + DATABASE_NAME, null, DATABASE_VERSION); 
     this.mcontext = context; 
} 

    public static String getpath(Context context){ 
     manager = (Manager) new ContextWrapper(context).getApplicationContext(); 
     String path = Environment.getExternalStorageDirectory() + "/" + "Manager"; 
     File dir = new File(path); 


     if(!dir.exists()) { 
      if (dir.canWrite()){ 
       dir.mkdirs(); 
      }else{ 
       manager.setDbLocation(dataFiles.Location.Phone); 
       return ""; 
      } 
     } 
     manager.setDbLocation(dataFiles.Location.SdCard); 
     path = path + "/"; 
     return path ; 
    } 
相關問題