Onimusha
Onimusha

Reputation: 3385

Android Write file to a different application folder

I'm trying to customize another app slightly by modifying some image files. So objective is to simply overwrite tiny jpg files (one at a time as required)

/data/data/com.otherapp/files/somefile.jpg for example needs to be overwritten

p = Runtime.getRuntime().exec("su"); is already run before I tried the write method.

I also added Runtime.getRuntime().exec("chmod 077 " +myFile.getAbsolutePath()); before the write code as suggested on a similar question

I'm getting permission denied. java.io.FileNotFoundException: /data/data/com......jpg: open failed: EACCES (Permission denied)

The write code I have is:

    //myDir is /data/data/com.......
    File myFile = new File (myDir.getAbsolutePath(), fname);
    try {
        //myFile.createNewFile();//tried with and without this
        FileOutputStream out = new FileOutputStream(myFile);
        btbmp.compress(Bitmap.CompressFormat.JPEG, 60, out);
        out.flush();
        out.close();
    } catch (Exception e) {
           e.printStackTrace();
    }

How can I do this?

Upvotes: 1

Views: 859

Answers (1)

Onimusha
Onimusha

Reputation: 3385

Okay it's taken all day but I've achieved the desired result thus:

  • Create file on SD card
  • Then copy file to root destination

src_file = "somefile/on/sdcard.jpg"

dest_file = "/data/data/com.anotherapp/files/abcde.jpg" got path using context

    try {
        Process process = Runtime.getRuntime().exec("su");

        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("rm "+dest_file + "\n"); //removes destination file -might not be required - haven't tested
        os.flush();
        os.writeBytes("cat "+src_file+" > "+dest_file + "\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();

        // Waits for the command to finish.
        process.waitFor();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }

Note In some cases I had to chmod the root folder 777 for it to work. I'm sure this isn't good practice and doing so will mean the app that uses that folder might not be able to access it. In my case that's what happened until I rectified it using ES Explorer

Upvotes: 2

Related Questions