Ray
Ray

Reputation: 568

How to get Android getExternalStorageDirectory folder by adb?

I want to generate a file while app is running thus my computer can monitor my app status in time(the cell phone is connected via usb all the time).

Is there anyway to let both adb and app point the same directory?

Also, If there has another way to make android can communicate with computer, please tell me.


Methods I tried:

At first, I though

echo $EXTERNAL_STORAGE

and

Environment.getExternalStorageDirectory()

would return the same value.

The value adb shell returned:

/storage/emulated/legacy

However, the result of calling Environment.getExternalStorageDirectory() In program is:

/storage/emulated/0

Then, I make android run shell command

        Process su = Runtime.getRuntime().exec("echo $EXTERNAL_STORAGE");
        //also tried "echo \\$EXTERNAL_STORAGE"
        su.waitFor();
        in = su.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine())!= null){
            result = result + line + "\n";
        }
        reader.close();
        in.close();
        su.destroy();
        return result;

but it return empty string.

Upvotes: 3

Views: 1584

Answers (2)

Ray
Ray

Reputation: 568

Finally, I got the value by following command.

Runtime.getRuntime().exec(new String[] { "sh", "-c", "echo $EXTERNAL_STORAGE" }); 

Thanks Alex P. inspiring me.

Upvotes: 1

Alex P.
Alex P.

Reputation: 31676

echo is not a separate executable but rather a shell's built-in command.

Use Runtime.getRuntime().exec("sh -c 'echo $EXTERNAL_STORAGE'"); instead

Upvotes: 1

Related Questions