2

I am trying to write an apk to /system/app location in Android Emulator. Here is my code:

File path = Environment.getRootDirectory();
File fileToWrite = new File(path, "/" + fileName);

byte[] buff = new byte[1024];
int l = 0;
// write buffer to file
FileOutputStream fos = new FileOutputStream(fileToWrite);
while((l = zis.read(buff)) > 0){
    fos.write(buff,0, l);
}
fos.close();

My AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

However, I am getting these error message:

java.io.FileNotFoundException: /system/MainApp-debug.apk (Read-only file system) 06-08 08:51:59.858 2921-3160/com.mainapp W/System.err: at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:287)

I managed to write to /mnt/sdcard/Download folder using the same code. But I not sure why it is not able to write to /system/app folder.

Any ideas? Thanks!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Your code is correct problem is with the emulator file system. The location you want to write is read only – aman5319 Jun 08 '18 at 08:57
  • Is there any way to change it? –  Jun 08 '18 at 08:58
  • Nope You try running the app in a mobile device if it runs there fine, its obvious emulator has a problem – aman5319 Jun 08 '18 at 09:00
  • Has nothing to do with an emulator. That path is read only. Start to root your device. – greenapps Jun 08 '18 at 09:02
  • @greenapps I see I see so the problem is because the emulator is not rooted? Is that the reason why I could not execute "su" command on it as well? –  Jun 08 '18 at 09:10
  • @aman5319 Do I need to root my mobile device as well? Because I do not have a rooted device –  Jun 08 '18 at 09:10
  • Well what is the reason you try to copy an .apk file to that folder? – greenapps Jun 08 '18 at 09:22
  • @guest176969 yes if you want to do something in root directory you have to root your phone. – aman5319 Jun 08 '18 at 15:32
  • @greenapps Because I thought when I copy the .apk to system folder, by rebooting the apps itself it is considered as installed as the latest version? –  Jun 09 '18 at 03:22
  • Your phantasy is huge. Better use an intent to install/update your app. – greenapps Jun 09 '18 at 08:00
  • @greenapps I see I see. But may I check with you regarding this thread: https://stackoverflow.com/questions/50751844/android-install-apk-programatically? I used the intent but I am not sure why it does not install at all :( –  Jun 09 '18 at 08:37
  • Post your intent. I saw nothing special in the link you referred to. – greenapps Jun 09 '18 at 09:15
  • @greenapps Yeap that is the intent I wrote in the question I posted. Is there any other intent that I am supposed to write in order to make it works? –  Jun 09 '18 at 16:01
  • In many occasions its better not to use a file provider. – greenapps Jun 09 '18 at 18:12
  • @greenapps I see I see. But any recommendations on how to implement the version upgrade and install without starting a new intent? Because I have a fragment to show successfully upgraded message so opening up a new intent might caused the UI transition to be awkward –  Jun 10 '18 at 04:59
  • Luckily that is not possible. Task for the OS and only if the user agrees. – greenapps Jun 10 '18 at 07:34
  • @greenapps But then do you have any ideas why it does not prompt user for permission? –  Jun 10 '18 at 23:54
  • Please post your code. – greenapps Jun 11 '18 at 06:16

1 Answers1

0

here is code that i used for create the path :

    public static File root = android.os.Environment
        .getExternalStorageDirectory();

public static String path = root.getAbsolutePath() + "/" + directoryName
        + "/";

public static boolean CreateDirectory() {

    boolean ret = false;
    path = root.getAbsolutePath() + "/" + directoryName + "/";
    File folderExisting = new File(root.getAbsolutePath(), directoryName);

    if (folderExisting.exists()) {
        ret = true;
    } else {
        ret = folderExisting.mkdir();
        // ret = false;
    }

    return ret;
}

and then for saving the things :

    public static boolean SaveToSD(List<String> data, String fileName) {

    // File root = android.os.Environment.getExternalStorageDirectory();
    boolean ret = false;
    CreateDirectory();
    File file = new File(path, fileName);

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        for (int idx = 0; idx < data.size(); idx++) {
            if (data.get(idx) != null)
                pw.println(data.get(idx));
        }

        pw.flush();
        pw.close();
        f.close();
        ret = true;
    } catch (FileNotFoundException e) {
        ret = false;
        e.printStackTrace();
    } catch (IOException e) {
        ret = false;
        e.printStackTrace();
    }

    return ret;

}

hope it helps you

and for an object:

        FileOutputStream fos = new FileOutputStream("file");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(mb);
        oos.close();
White Druid
  • 295
  • 1
  • 12
  • I see I see but does it required a rooted device? What you did was creating a directory under system/app folder then store the file into it, am I right? –  Jun 09 '18 at 03:24
  • i dont know does or doesnt but generally you are right test it please – White Druid Jun 09 '18 at 08:13
  • Hey may I know what is the data parameter is for? Because so far I only have a File object –  Jun 10 '18 at 23:56
  • so then use anything you want insted List it will work fine you should delet some part of this code use ObjectOutputStream – White Druid Jun 11 '18 at 12:42