【问题标题】:copy a database file programatically from asset folder of the application以编程方式从应用程序的资产文件夹复制数据库文件
【发布时间】:2011-12-13 12:35:42
【问题描述】:

我已经搜索了很多关于这个问题。我的手机没有root。我想以编程方式将数据库文件从应用程序的资产文件夹复制到我的 Android 设备的 /system/usr。这样我就可以从那里访问数据库文件并检查我的应用程序的用户是否能够升级数据库。我知道我必须更改以下代码中的“outFileName”和“outputStream”,但我不确定如何在以下代码中指定 /sytem/usr 的输出路径:

copyDBfromassetstodevicedrive.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try {
                    // Open your local db as the input stream
                    InputStream myInput = myContext.getAssets().open("myDB.db");

                    // Path to the just created empty db
                    String outFileName = "/data/data/your app package name/databases/database file name";

                    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();
                    } 
                    catch (Exception e) 
                    {
                    Log.e("error", e.toString());
                    }

            }
          });

感谢您的任何建议。

【问题讨论】:

    标签: android ddms android-assets


    【解决方案1】:

    除非您的设备已植根,否则这是不可能的。但是我不明白你为什么需要这样做。也许如果你能说出你想做什么,这里有人可以帮你找到解决方案。

    【讨论】:

    • 即使以编程方式也不可能?我的应用程序允许用户在运行应用程序时向 sqlite 数据库添加新行。我想看看用户是否真的能够添加新行。我想可以使用 sql 查询来完成,例如:stackoverflow.com/questions/4017903/…
    • 您可以从 assets 文件夹复制到 data/data/com.yourname/databases 但不能复制到 system/usr。前者能解决你的问题吗?
    • 无法将文件复制到您要放置的位置 (/system/usr)。但您当然可以将其复制到应用程序私有(内部)存储或设备外部存储。请参阅this 了解更多信息。
    • 嗨,我有同样的问题:目的是我的数据库值变化非常频繁,我从外部源获取数据库文件,所以我想将该文件放入 sd卡并访问 SD 卡中的值。总之我不希望db文件存储到data/data/pacagename/database,我希望数据库的默认位置到SD卡。
    【解决方案2】:

    private static String DB_PATH = Environment.getDataDirectory()+"/data/package-name/databases/";
    
    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 d inb
        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();
    
        //Copy successful
        outFileName = DB_PATH + DB_SUCCESS;
        myOutput = new FileOutputStream(outFileName);
        myOutput.write(1);
        myOutput.flush();
        myOutput.close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 2015-08-22
      相关资源
      最近更新 更多