Page 1 of 1

Android Shell Code

Posted: Sat Mar 14, 2020 4:12 pm
by thecoolpeople
Hello Programmers:

I have some cool code pieces, which can help many of you! All of these are for android, so have fun. Of course, there are many more, but I think, these are the important ones. If you have some more questions, please write them below! :awesome:
When you have some improvements, write me a private message, or below.

I will improve and find more of such cool commands, and I will share them with you!

Open file with the standard program: (by thecoolpeople)

Code: Select all

local openFile = function(path, mimetype)
  path = love.filesystem.getSaveDirectory() .. "/" .. path
  path = "file://" .. path
  os.execute("am start --user 0 -a android.intent.action.VIEW -d " .. path .. " -t " .. mimetype)
end
Share file: (by thecoolpeople)

Code: Select all

local shareFile = function(path, mimetype)
  path = love.filesystem.getSaveDirectory() .. "/" .. path
  path = "file://" .. path
  os.execute("am start --user 0 -a android.intent.action.SEND -t " .. mimetype .. " --eu android.intent.extra.STREAM " .. path)
end

Re: Android Shell Code

Posted: Thu Mar 26, 2020 12:58 am
by Kunologist
Woah, so happy to see that os.execute() is working on LOVE Android!
So I guess os.time() and os.date() would work too!

Re: Android Shell Code

Posted: Thu Mar 26, 2020 2:40 am
by AuahDark
Beware that code won't work in Android Nougat and later due to file:// URI banned in that and later version of Android. Of course all os.* functions work as they should, only limited by system permissions.

Re: Android Shell Code

Posted: Thu Mar 26, 2020 4:33 pm
by Kunologist
And I found a tiny bit more interesting below os.execute(). With this function we can make pur LOVE game write contents to external directories without root permission. The idea is the Linux behind every Android device.

If you want to create an external directory, use:

Code: Select all

os.execute("mkdir /sdcard/lovegame/savedata/")
(The path is just an instance of using mkdir. Avoid using /sdcard, it's a bad practice after all.)

And to write a file (if doesn't exist, will create it), use:

Code: Select all

os.execute([[echo 'Hello World!' >/sdcard/lovegame/savedata/helloworld.txt]])
And that's pretty amazing given that 11.3 or Android still left us with a backdoor to write external files.

Bad thing I haven't found out how to read, so bleh. Any ideas?

Re: Android Shell Code

Posted: Fri May 15, 2020 8:42 am
by Guard13007
Can you please explain why writing to /sdcard is considered bad practise and what we should use instead? Just stating not to do it without an alternative isn't very helpful. :(