2011-07-21 13 views
0

私は、データベース・コードを作成する書かれている;私が取得ERROR/AndroidRuntime(7193):com.android.xont.db.DataBaseHelper.createDataBaseで(DataBaseHelper.java:43)Androidデータベースの作成エラーです。

は、これは私のコードです:

public class DataBaseHelper extends SQLiteOpenHelper { 
    private static String DB_PATH = "/data/data/com.android.xont.controller/databases/"; 
    private static String DB_NAME = "DEMOUserVentura_HEMA.db"; 
    private SQLiteDatabase DEMOUserVentura_HEMA; 
    private final Context myContext; 


    private DataBaseHelper myDbHelper; 

public DataBaseHelper(Context context) { 
    super(context, DB_NAME, null, 1); 
    this.myContext = context; 
} 

/** 
* Creates a empty database on the system and rewrites it with your own 
* database. 
**/ 
public void createDataBase() throws IOException { 
    boolean dbExist = checkDataBase(); 
    if (dbExist) { 
    } else { 
     this.getReadableDatabase().close(); 
     //this.getReadableDatabase().getPath(); 
     //System.out.println(" ===== " + this.getReadableDatabase().getPath()); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error(e); 
     } 
    } 

} 

private boolean checkDataBase() { 
    SQLiteDatabase checkDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 
     // database does't exist yet. 
    } 

    if (checkDB != null) { 
     checkDB.close(); 
    } 

    return checkDB != null ? true : false; 
} 

private void copyDataBase() throws IOException { 

    // Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 

    // Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    // transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) { 
     myOutput.write(buffer, 0, length); 
    } 

    // Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

public void openDataBase() throws SQLException { 

    // Open the database 
    String myPath = DB_PATH + DB_NAME; 
    DEMOUserVentura_HEMA = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.OPEN_READONLY); 

} 

@Override 
public synchronized void close() { 
    if (DEMOUserVentura_HEMA != null) 
     DEMOUserVentura_HEMA.close(); 
     super.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
} 
} 

このアクティビティを呼び出すと、例外が発生します。データベースのサイズは4.55MBです。

getReadableDatabase()。getPath()は、dbパスを正しく返します。

私を助けてください。私は一週間一本の髪を引っ張ってきた。

エミュレータを実行すると、少量のファイルがコピーされます。すべてではない。私は手作業でコピーをしました。

今問題はHTC電話です。その例外を修正する方法。私を助けてください。事前に

enter image description here

おかげ..

答えて

2

AssetManagerは、ファイルサイズの制限があります。一部のデバイスでは、1MBを超えるアセットファイルは開けません。ファイルを小さな塊で分割し、それらをアセットフォルダに入れ、アプリが動作しているときに塊を再構成する必要があります。

チャンクを再構成する方法については、this questionを参照してください。

OSXまたはLinuxでチャンクを作成するには、コマンドラインツール 'split'を使用できます。

hereまたはhere参照するか、DBは非常に最初の時間をopenendされる資産フォルダからチャンクを再構成DatabaseHelperを実装する方法についてのチュートリアルを見つけるために、キーワード「アンドロイドdatabasehelper資産」でGoogleを使用しています。

+0

リンクありがとうございます。そのcreateNewFile()メソッドは何ですか?あなたは完全なソースコードを持っていますか? – Piraba

+0

サンプルコードに関する編集を参照してください – thaussma

+0

アンドロイド2.3.3を使用する場合、チャンクを行う必要はありませんか? – Piraba

関連する問題