How to access Android filesystem with Lua?

I want to create mod system for my game. My idea is to put mods folder inside the /sdcard/ directory, so user can easy install mods with file manager.
I modified my game via ApkTool to ask for MANAGE_EXTERNAL_STORAGE permission, and that permission has been successfully granted.

But when I added following code, my game just frozen without creating directory.

My code:

local lfs = require("lfs")

lfs.mkdir("/sdcard/myGame")

I think the native is limited to the solar2d sandbox.

The is a plugin.
https://scotth.tech/plugin/externalStorage (cost $9.99 )

Any free solutions?

Yes! Use your own domain’s web space

Hi ryzhpolsos,

Welcome to the Solar2D forum. I rolled my own here. Not sure if it would work for you.

Try this path, “/storage/emulated/0/myGame”. It points to Android “external” storage (outside the Android system) which is the storage that comes built-in with the device, and not the sdcard that you add & insert to your device. Of course, “myGame” is your created folder on this storage.

It works with all my devices, 2 x LG phones, 2 x Samsung tablets, 2 x Samsung phones, ranging from very old device (~8 years) to recent models.

You would need to have the following in the “build.settings”. I hope this helps. Wish you all the best !

	--
	-- Android section
	--
	android = {
		usesPermissions = {
			"android.permission.INTERNET",
			"android.permission.READ_EXTERNAL_STORAGE",
			"android.permission.WRITE_EXTERNAL_STORAGE",
		},
	},
2 Likes