<bdo id='995kW'></bdo><ul id='995kW'></ul>

<tfoot id='995kW'></tfoot>
  • <small id='995kW'></small><noframes id='995kW'>

      <legend id='995kW'><style id='995kW'><dir id='995kW'><q id='995kW'></q></dir></style></legend>

        <i id='995kW'><tr id='995kW'><dt id='995kW'><q id='995kW'><span id='995kW'><b id='995kW'><form id='995kW'><ins id='995kW'></ins><ul id='995kW'></ul><sub id='995kW'></sub></form><legend id='995kW'></legend><bdo id='995kW'><pre id='995kW'><center id='995kW'></center></pre></bdo></b><th id='995kW'></th></span></q></dt></tr></i><div id='995kW'><tfoot id='995kW'></tfoot><dl id='995kW'><fieldset id='995kW'></fieldset></dl></div>

        Sqlite 数据库未从资产文件夹 Android 复制

        时间:2024-04-15

      1. <legend id='GFEwN'><style id='GFEwN'><dir id='GFEwN'><q id='GFEwN'></q></dir></style></legend>

            <tbody id='GFEwN'></tbody>
        1. <i id='GFEwN'><tr id='GFEwN'><dt id='GFEwN'><q id='GFEwN'><span id='GFEwN'><b id='GFEwN'><form id='GFEwN'><ins id='GFEwN'></ins><ul id='GFEwN'></ul><sub id='GFEwN'></sub></form><legend id='GFEwN'></legend><bdo id='GFEwN'><pre id='GFEwN'><center id='GFEwN'></center></pre></bdo></b><th id='GFEwN'></th></span></q></dt></tr></i><div id='GFEwN'><tfoot id='GFEwN'></tfoot><dl id='GFEwN'><fieldset id='GFEwN'></fieldset></dl></div>
            <bdo id='GFEwN'></bdo><ul id='GFEwN'></ul>

            • <tfoot id='GFEwN'></tfoot>
              1. <small id='GFEwN'></small><noframes id='GFEwN'>

                  本文介绍了Sqlite 数据库未从资产文件夹 Android 复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将名为adinpect"的数据库从资产文件夹复制到应用程序数据库文件夹,但它不起作用...

                  I am trying to copy a database named "adinpect" from the asset folder to the application databases folder, but it is not working...

                  代码(在主要活动 onCreate() 中,仅用于测试):

                  Code (in main activity onCreate(), just for testing):

                   try {
                          String destPath = "/data/data/" + getPackageName() + "/databases";
                          File f = new File(destPath);
                  
                          if (!f.exists()) {
                  
                              f.mkdirs();
                              f.createNewFile();
                              //---copy the db from the assets folder into the databases folder---
                              CopyDB(getBaseContext().getAssets().open("adinspect"), new FileOutputStream(destPath + "/adinspect"));
                          }
                  
                      } 
                          catch (FileNotFoundException e) {
                              e.printStackTrace();
                      }   catch (IOException e) {
                              e.printStackTrace();
                      }
                  
                  
                   public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException {
                  
                          //---copy 1K bytes at a time---
                          byte[] buffer = new byte[1024];
                          int length;
                          while ((length = inputStream.read(buffer)) > 0) {
                              outputStream.write(buffer, 0, length);
                          }
                  
                          inputStream.close();
                          outputStream.close();
                  
                  }//end copyDB
                  

                  数据库"文件夹已创建,但其中没有任何内容,正在尝试通过 DDMS 访问它.

                  The "databases" folder is created but there in nothing in it, trying to access it by DDMS.

                  我没有收到任何错误.

                  有什么建议吗?

                  谢谢

                  推荐答案

                  此代码将帮助您从 assets 文件夹中复制 DB.您可以先检查数据库是否存在.

                  This code wlll help you to copy DB from assets folder. You can check first if DB exists or not.

                  try{
                      // CHECK IS EXISTS OR NOT
                      SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+getPackageName+"/databases/dbname.sqlite",null, 0);
                      dbe.close();
                  }
                  catch(Exception e)}
                  {
                      // COPY IF NOT EXISTS
                      AssetManager am = getApplicationContext().getAssets();
                      OutputStream os = new FileOutputStream("/data/data/"+getPackageName+"/databases/dbname.sqlite");
                      byte[] b = new byte[100];
                      int r;
                      InputStream is = am.open("dbname.sqlite");
                      while ((r = is.read(b)) != -1) {
                           os.write(b, 0, r);
                     }
                     is.close();
                     os.close();
                  }
                  

                  这篇关于Sqlite 数据库未从资产文件夹 Android 复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQLite 数据库副本在由设备而不是模拟器生成时出现损坏 下一篇:Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性)

                  相关文章

                    <bdo id='xN5v0'></bdo><ul id='xN5v0'></ul>
                      <tfoot id='xN5v0'></tfoot>
                    1. <i id='xN5v0'><tr id='xN5v0'><dt id='xN5v0'><q id='xN5v0'><span id='xN5v0'><b id='xN5v0'><form id='xN5v0'><ins id='xN5v0'></ins><ul id='xN5v0'></ul><sub id='xN5v0'></sub></form><legend id='xN5v0'></legend><bdo id='xN5v0'><pre id='xN5v0'><center id='xN5v0'></center></pre></bdo></b><th id='xN5v0'></th></span></q></dt></tr></i><div id='xN5v0'><tfoot id='xN5v0'></tfoot><dl id='xN5v0'><fieldset id='xN5v0'></fieldset></dl></div>

                      <small id='xN5v0'></small><noframes id='xN5v0'>

                    2. <legend id='xN5v0'><style id='xN5v0'><dir id='xN5v0'><q id='xN5v0'></q></dir></style></legend>