2011-11-23 5 views
1

어떤 이유로 든 자산 폴더의 데이터베이스를 에뮬레이터 또는 일부 전화기 (대부분의 전화기에서 작동)의 데이터베이스 지시문으로 복사하려고 할 때마다 충돌이 발생합니다. 나는이 코드 조각으로 그것을 좁혔다. 루프 동안 실제에 도달 할 때까지루프에서 InputStream이 충돌 함

 //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(); 

Heres는 헬퍼 클래스

public class NewDbHelper extends SQLiteOpenHelper { 
//The Android's default system path of your application database. 
private static String DB_PATH = "/data/data/com.bv.studyguide/databases/"; 
private static String DB_NAME = "studyguide.db"; 
private SQLiteDatabase myDataBase; 
private final Context myContext; 
private static int DB_VERSION = 2; 
private SQLiteDatabase db; 
private Cursor cursor; 

/** 
* Constructor 
* Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
* @param context 
*/ 
public NewDbHelper(Context context) { 

    super(context, DB_NAME, null, DB_VERSION); 
    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) { 
     Log.v("DB Exists", "db exists"); 
     // By calling this method here onUpgrade will be called on a 
     // writeable database, but only if the version number has been 
     // bumped 
     this.getWritableDatabase(); 
     } 

    dbExist = checkDataBase(); 

    if (!dbExist) { 
     // By calling this method and empty database will be created into 
     // the default system path of your application so we are gonna be 
     // able to overwrite that database with our database. 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 


} 

/** 
* Check if the database already exist to avoid re-copying the file each time you open the application. 
* @return true if it exists, false if it doesn't 
*/ 
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; 
} 

/** 
* Copies your database from your local assets-folder to the just created empty database in the 
* system folder, from where it can be accessed and handled. 
* This is done by transfering bytestream. 
* */ 
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; 
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

} 

@Override 
public synchronized void close() { 

     if(myDataBase != null) 
      myDataBase.close(); 

     super.close(); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE android_metadata (locale TEXT)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    if (newVersion > oldVersion) 
     Log.v("Database Upgrade","Database version higher than old."); 
     myContext.deleteDatabase(DB_NAME); 
} 

코드에 대한 전체 코드는 잘 실행됩니다. 루프를 입력하지 않으면 IO 예외가있는 while 회선에서 충돌이 발생합니다. 그게 무슨 뜻인지 모르겠다. 그리고 Im은 InputStream과 OutputStream에 정통하지 않으므로, 문제는 무엇인지 알 수 없다. 내 Droid X 및 Droid 2에서는 정상적으로 작동하지만 Droid 1 또는 LG670에서는 정상적으로 작동하지 않습니다. 누구든지 아이디어가 있습니까?

Heres는 스택 추적 :

java.lang.Error: Error copying database 
at com.bv.studyguide.NewDbHelper.createDataBase(NewDbHelper.java:63) 
at com.bv.studyguide.ArmyStudyGuide.onCreate(ArmyStudyGuide.java:39) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) 
at dalvik.system.NativeStart.main(Native Method) 

UPDATE : 임은 또한 동일한 코드 (실질적으로) 또 다른 응용 프로그램에 문제로 실행. 예외를 제외하고는 데이터베이스를 복사 할 때 예외입니다. 전체 데이터베이스를 복사하지만 데이터베이스에 아무것도 넣지 않기로 결정합니다. 따라서 3072 바이트를 읽지 만, 그 안에있는 유일한 테이블은 android_metadata 테이블입니다. 정말로 혼란스러워. 이 바이트 배열이 문제의 원인입니까?

+2

예외를 포함하십시오. –

+0

예외 세부 정보 pls. 잠정적으로 myInput에 몇 가지 문제가있을 수 있지만 예외 –

+0

을 "IO 예외로 인해 충돌 ... 연결하면 더 명확 해집니다. 그게 무슨 뜻인지는 잘 모릅니다." 우리는 그것이 의미하는 것이 무엇인지 모릅니다. 예외를 게시 할 때까지는 알 수 없습니다. – EJP

답변

0

나는 문제를 지적했다. 진저 브레드 안드로이드가 자산 폴더에서 1GB가 넘는 파일에 액세스 할 수 없게하기 전에. 그래서 파일을 조각 냈고 실행 시간에 htem을 다시 넣는 메서드를 만들었습니다.