2013-07-10 2 views
0
여기

에서 복사 데이터베이스 (많은 답변에 있음) 내가 사용하고 코드입니다 : 그러나안드로이드 - 자산

InputStream myInput; 
     try { 
      myInput = iNezamApplication.getAppContext().getAssets().open(DB_NAME); 
      String outFileName = DB_PATH + DB_NAME; 

      OutputStream myOutput = new FileOutputStream(outFileName); 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer))>0){ 
       myOutput.write(buffer, 0, length); 
      } 

      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 

     } catch (IOException e1) { 
      e1.printStackTrace(); 

     } 

, 난 항상 OutpoutStream 선에 도달 한 후 예외가 :

java.io.FileNotFoundException: /data/data/package_name/databases/databasename.db: open failed: ENOENT (No such file or directory) 
+3

노인보다는, 테스트, 디버깅 및 지원'SQLiteAssetHelper'로 전환 고려하시기 바랍니다 , 지원되지 않는 대구 전자 "많은 답변에서 발견됨": https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare

+0

감사합니다. 알고 있지만 사용하고 싶지 않습니다. 나는 내 질문에 대한 대답이 필요하다. –

답변

6

나는 이런 식으로 뭔가를 시도에서 파일 개체를 만들어야합니다 ..

final String[] sample_dbName = {"DrugsNew.db"}; 

    int assetDbSize = sample_dbName.length; 
    File databaseFile = new File("/data/data/com.handyrep2.ui/databases"); 

    // check if databases folder exists, if not create one and its subfolders 
    if (!databaseFile.exists()){ 
     databaseFile.mkdir(); 
    } 

     for(int i=0;i<assetDbSize;i++){ 
      String outFilename =null; 

     outFilename = "/data/data/com.handyrep2.ui/databases"+ "/" + sample_dbName[i]; 

      File sampleFile = new File(outFilename); 

       try { 
        InputStream in = activity.getAssets().open("offlinedb/"+sample_dbName[i]); 

        OutputStream out = new FileOutputStream(outFilename); 
        // Transfer bytes from the sample input file to the sample output file 
        byte[] buf = new byte[1024]; 
        int len; 
        while ((len = in.read(buf)) > 0) { 
         out.write(buf, 0, len); 
        } 
        out.flush(); 
        // Close the streams 
        out.close(); 
        in.close(); 
       }catch(IOException e) { 

       } 


     } 
+0

먼저 데이터베이스 디렉토리를 만드셔야합니다. –

1

을 "package_name"은 실제 패키지 이름 대신 여기에 게시하거나 DB_PATH에서 실제로 사용합니까? :)

+0

아니요, 실제 패키지 이름입니다. 여기서는 생략했습니다 :) –

+0

OK. 나도 몰라. 우리는 현재 프로젝트에서 거의 동일한 접근 방식을 사용하고 있습니다. 제대로 작동하기 때문에 지금까지 어떤 오류도 볼 수 없습니다. 죄송합니다 : ( – a11n

1

은 먼저

+0

도 작동하지 않았습니다. –

관련 문제