【问题标题】:Loading database from asset folder - Fileoutputstream fails从资产文件夹加载数据库 - Fileoutputstream 失败
【发布时间】:2014-05-15 18:09:35
【问题描述】:

我的问题是当数据库从资产文件夹复制到手机路径时,我的应用程序总是失败:

/data/data/at.atn.android/databases/

我的数据库名称:

atnRoutenplaner.sqlite3

我的转账代码:

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;
    File sampleFile = new File(outFileName);
    //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();

}

【问题讨论】:

    标签: android assets fileoutputstream


    【解决方案1】:

    试试这个,

     public void CopyDataBaseFromAsset() throws IOException{
           InputStream in  = ctx.getAssets().open("mycontacts");
           Log.e("sample", "Starting copying" );
           String outputFileName = DATABASE_PATH+DATABASE_NAME;
           File databaseFile = new File( "/data/data/com.copy.copydatabasefromasset/databases");
            // check if databases folder exists, if not create one and its subfolders
            if (!databaseFile.exists()){
                databaseFile.mkdir();
            }
    
           OutputStream out = new FileOutputStream(outputFileName);
    
           byte[] buffer = new byte[1024];
           int length;
    
    
           while ((length = in.read(buffer))>0){
                  out.write(buffer,0,length);
           }
           Log.e("sample", "Completed" );
           out.flush();
           out.close();
           in.close();
    
        }
    

    【讨论】:

    • 抱歉没用。我不明白为什么。我使用了以前的数据库和 atnRoutenplaner.sqlite 。也许这就是结局?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多