1

我已经搜索了很多关于这个问题。我的手机没有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());
                    }

            }
          });

感谢您的任何建议。

4

2 回答 2

1

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

于 2011-12-13T13:32:36.180 回答
0

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();
}
于 2013-07-25T19:08:01.943 回答