5

I am trying to mount the USB mass storage device to my Raspberry Pi running the android things. I came across this answer which shows how to mount it using command line ADB shell. But the problem is I have to run those command every time my device boots. I want to mount the USB drive in onCreate() of my launch activity. Here is the code:

//Here is the mount drive function which I called in onCreate of my activity.

 private void mountDrive() throws IOException, InterruptedException {
        Process mProcess = Runtime.getRuntime().exec("/system/xbin/su");
        BufferedReader reader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));

        DataOutputStream dos = new DataOutputStream(mProcess.getOutputStream());
        dos.writeBytes("mkdir /mnt/usb\n");
        dos.flush();
        dos.writeBytes("mount -t vfat -o rw /dev/block/sda1 /mnt/usb\n");
        dos.flush();
        dos.writeBytes("exit\n");

        //Read the response
        String line, result = "";
        while ((line = reader.readLine()) != null) {
            result += line;
            Log.d("CMD","RESULT:"+result);
        }
        reader.close();
        dos.flush();
        dos.close();

        mProcess.waitFor();
    }

But I am getting this error:

I/sh: type=1400 audit(0.0:31): avc: denied { read } for name="/" dev="mmcblk0p6" ino=2 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:rootfs:s0 tclass=dir permissive=1
I/sh: type=1400 audit(0.0:32): avc: denied { open } for path="/" dev="mmcblk0p6" ino=2 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:rootfs:s0 tclass=dir permissive=1
W/System.err: java.io.IOException: Cannot run program "su": error=13, Permission denied
W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
W/System.err:     at java.lang.Runtime.exec(Runtime.java:691)
W/System.err:     at java.lang.Runtime.exec(Runtime.java:524)
W/System.err:     at java.lang.Runtime.exec(Runtime.java:421)

How can I mount the USB device using from my application on Android Things?

Community
  • 1
  • 1
Keval Patel
  • 592
  • 3
  • 13
  • 1
    Under Android it is mounted automatically. So i wonder why this would not happen under Android Things. – greenapps May 09 '17 at 09:34

3 Answers3

1

You only can run these commands in a rooted Android. Android Things (as Android) does not allow to execute shell commands by security reasons.

  • Your answer is **completely wrong!** `Android Things` image is shipped with `su` binary by default (at least by now) which is quite logical taking into consideration that the OS is intended for embedded systems. Even more, you can execute a lot of [shell commands (incl. programmatically)](https://stackoverflow.com/a/44145193/3290339) since **most of them have world-executable permission**. – Onik Sep 15 '17 at 22:42
  • Yes you can run su commands from shell or via adb, but you cant do it programatically. At least I always get that "permission denied". – Salva Domingo Sep 15 '17 at 23:12
  • You haven't read the answer I refereed attentively. No need to grab `su` programmatically, - **most of the binaries under `/system/bin` are world-executable**. [Here is another example](https://stackoverflow.com/a/44209713/3290339). Why don't you try it?! – Onik Sep 15 '17 at 23:19
1

I had the exact same issue that you have. You cannot execute the adb command from within your app.

However you can access the USB via Android's Usb Host API, and therefore the libaums library works to access the Usb mass storage device - given that the mass storage device uses a FAT32 filesystem.

Phaestion
  • 63
  • 8
1

An alternative is to modify the init.rc file. Search the init.rc file for on property: sys.boot_completed = 1:

on property: sys.boot_completed = 1
bootchart stop
  # WLD 201805031702
  mkdir / mnt / usb
  chmod 777 / mnt / usb
  exec / system / bin / mount -t vfat / dev / block / sda1 / mnt / usb

#operate only if the pendrive is placed before powering on, if you remove and put it will not work
Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
W PLASSA
  • 11
  • 4