1

テキスト ドキュメントを apk から /system ディレクトリにプッシュしたいと思います (はい、ルート化されたユーザー向けのアプリです)。これをどのように行うのか疑問に思っていました :) 私の txt ファイルは assests フォルダーにありますが、必要に応じて使用できます。

4

2 に答える 2

3

プロジェクトのアセット ディレクトリにテキスト ファイルを配置し、次のスレッドのようなコードを使用してファイル システムに抽出します

編集:これは私が使用するコードです。sourceFileName には、アセット フォルダーに相対的なアセット ファイルの名前を渡します (たとえば、アセット フォルダーに myFile.txt がある場合は、myFile.txt を渡します)。宛先ファイルには、フル パスを渡します (例: /data/data/com.mycompany/mypackage/myFile.txt)。context は現在のアクティビティです (例: MyActivity.this)。

private boolean copyFile(Context context, String sourceFileName, String destFileName)
{
    AssetManager assetManager = context.getAssets();

    File destFile = new File(destFileName);

    File destParentDir = destFile.getParentFile();
    destParentDir.mkdir();

    InputStream in = null;
    OutputStream out = null;
    try
    {
        in = assetManager.open(sourceFileName);
        out = new FileOutputStream(destFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;

        return true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return false;
}

EDIT2:ルート化されたデバイスであっても、/system パーティションが読み取り専用としてマウントされていることが判明しました。Android : APK 内からファイルシステムを RW にマウントする方法は? (もちろんroot化)

于 2012-05-16T17:52:38.063 に答える
0

この機能を試すことができます(ここにあります):

public String runSystemCommand(String cmd)
{
    try {
        // Executes the command.
        Process process = Runtime.getRuntime().exec(cmd);

        // Reads stdout.
        // NOTE: You can write to stdin of the command using
        //       process.getOutputStream().
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();

        // Waits for the command to finish.
        process.waitFor();

        return output.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }       
}

私はこの方法で自分で試しました:

    String cmdoutput = this.runSystemCommand("/system/bin/ls .");
    Log.d("SampleAndroidInterfaceActivity", "runSystemCommand() returned: " + cmdoutput);

そしてうまくいきます。これは私の出力です:

05-16 17:50:10.423: runSystemCommand() returned: acct
05-16 17:50:10.423: cache
05-16 17:50:10.423: config
05-16 17:50:10.423: d
05-16 17:50:10.423: data
05-16 17:50:10.423: default.prop
05-16 17:50:10.423: dev
05-16 17:50:10.423: etc
05-16 17:50:10.423: init
05-16 17:50:10.423: init.goldfish.rc
05-16 17:50:10.423: init.rc
05-16 17:50:10.423: mnt
05-16 17:50:10.423: proc
05-16 17:50:10.423: root
05-16 17:50:10.423: sbin
05-16 17:50:10.423: sdcard
05-16 17:50:10.423: sys
05-16 17:50:10.423: system
05-16 17:50:10.423: ueventd.goldfish.rc
05-16 17:50:10.423: ueventd.rc
05-16 17:50:10.423: vendor

txt ファイルの絶対パスがわかっている場合は、.txt ファイルを使用して簡単にコピーできますcp

于 2012-05-16T18:02:55.230 に答える