1

I would like to push a text document from the apk into the /system directory (Yes its a app for rooted users) and was wondering how i would do this :) My txt file is in the assests folder but it could be used if needed

CarbonAssassin
  • 855
  • 12
  • 24
  • 1
    Not really,i would like to now how i would like to go about copying a txt document from within my apk to the /system directory. How is it vague? (Honestly not meaning to sound rude) – CarbonAssassin May 16 '12 at 17:45
  • Alot of stuff i'd need to ask which is simple bits but all the android chat rooms are locked for some stupid reason! There is also no info online (I have been googling!) – CarbonAssassin May 16 '12 at 17:50
  • First of all push was a poor choice of term. You also do not specify what you have done or tried until now. – OlivierLi May 16 '12 at 17:51
  • ah ok, Well i have no idea where to start. I literally want to copy a file to the /system from my apk. Where would i start? – CarbonAssassin May 16 '12 at 17:52
  • possible duplicate of [Pushing a file from within a apk](http://stackoverflow.com/questions/10622497/pushing-a-file-from-within-a-apk) – CharlesB May 22 '12 at 15:41

2 Answers2

3

Place the text file in your project's assets directory and then extract it to the filesystem with code like the one in the following thread: How to copy files from 'assets' folder to sdcard?

EDIT: Here is some code that I use. For sourceFileName, pass in the name of the assets file relative to the assets folder (e.g. if you have myFile.txt in the assets folder, pass myFile.txt). For the destination file, pass a full path (e.g. /data/data/com.mycompany/mypackage/myFile.txt). context is the current activity (e.g. 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: Turns out that the /system partition is mounted as read-only, even on rooted devices. This may help: Android: how to mount filesystem in RW from within my APK? (rooted, of course)

Community
  • 1
  • 1
Theo
  • 5,963
  • 3
  • 38
  • 56
  • In the destination file is that just the actual file eg. /media/txt.txt or would it ust be /media/ (Those are made up directorys and file names XD) – CarbonAssassin May 16 '12 at 18:23
  • It's the actual file paths on the filesystem. Something else I remembered is that /system is mounted as read-only during normal operation, even on rooted devices. So you'd need to remount it to read-write: http://stackoverflow.com/questions/5481395/android-how-to-mount-filesystem-in-rw-from-within-my-apk-rooted-of-course – Theo May 16 '12 at 18:29
  • yeah i knew i'd have to do that, I'm struggling to place this code inside a onclick listener for some reason, Any help? – CarbonAssassin May 16 '12 at 18:32
  • I changed the function code to include the context in the function definition. Simply call the function from the onClick listener as `copyFile(MyActivity.this, "myFile.txt", "/system/myFile.txt")`. Should work if you took care of the read-write partition mounting. – Theo May 16 '12 at 18:38
  • Just trying it now :) Thanks alot for the support! – CarbonAssassin May 16 '12 at 18:40
  • Thanks alot :) i appreciate not only you helping me but you making a resource that other people can use with lots of links and alternative code. Its people like you that make websites like these better – CarbonAssassin May 16 '12 at 18:57
  • Thanks for the kind words. Glad I could help. :) – Theo May 16 '12 at 19:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11345/discussion-between-carbonassassin-and-theo) – CarbonAssassin May 16 '12 at 19:58
0

You can try this function (found here):

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);
    }       
}

I've tryed it myself in this way:

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

and works well. This is my output:

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

If you know the absolute path of your txt file, you can easily copy it with a cp.

Avio
  • 2,700
  • 6
  • 30
  • 50