1

私はこの問題についてたくさん検索しました。私の電話は根付いていません。アプリケーションのアセットフォルダーから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 に答える