Makedir on SD card failed

Discuss any general programming issues here
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Makedir on SD card failed

Post by amyren »

I try to make my app to create a directory on my SD card, but it fails with the error message:
"Error creating directory /storage/emulated/0/mydir !"
And it points to the line 311 in my script, which is the makedir line

Code: Select all

		t = GetVersion()
		If t.platform = "Android"
			If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
				t = GetSystemInfo()
				mydir$ = t.SDCard.."/mydir"
				MakeDirectory(mydir$)
User avatar
airsoftsoftwair
Posts: 5626
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Makedir on SD card failed

Post by airsoftsoftwair »

Which Android version is that?
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

This was on android 9, but I also get the same error on my tablet, which runs android 8.1.0, and on my old phone with android 4.2.1.
I also did a test by using t.t.ExternalStorage instead of t.SDcard, but this also result in a similar error.

Using ES-fileExplorer I do notice that if I stay at folder which apear to be /storage/emulated/0, and then go up one directory then I will be at /storage/emulated which is empty
Googling this issue reveal other usersdebating this issue https://forums.androidcentral.com/samsu ... empty.html, and it may look like google did some changes some time after android 6, that makes the directory protected in some cases.

Now as a workaround, I did test to just use /mnt/sdcard as the path instead, which does seem to work on the three devices I have tested.

BTW. I miss an Android section at this forum. You do have the APK Compiler section,but as it is labeled today, it seemes to be for compiler issues only. The newly created Remedios section is labeled more generally to cover all iOS related stuff, perhaps the APK Compiler section could be labeled a similar way?
User avatar
airsoftsoftwair
Posts: 5626
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Makedir on SD card failed

Post by airsoftsoftwair »

Does it also happen when you compile your script as an applet and run it through the Hollywood Player?
BTW. I miss an Android section at this forum. You do have the APK Compiler section,but as it is labeled today, it seemes to be for compiler issues only. The newly created Remedios section is labeled more generally to cover all iOS related stuff, perhaps the APK Compiler section could be labeled a similar way?
Well, I don't have platform-specific forums so I don't want to break this rule because otherwise there'll be people asking for a macOS forum, a Windows forum, a Linux forum, etc. That's just too many...
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

Yes, it also happens with the player
User avatar
airsoftsoftwair
Posts: 5626
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Makedir on SD card failed

Post by airsoftsoftwair »

Please post a minimal (!) test script, the code in the original post seems like an excerpt from something bigger...
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

airsoftsoftwair wrote: Tue Oct 01, 2019 11:24 am Please post a minimal (!) test script, the code in the original post seems like an excerpt from something bigger...
the code in the original post actually only need a couple of endif's to run. But here is an even smaller piece of code sufficient for testing

Code: Select all

If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
	t = GetSystemInfo()
	MakeDirectory(t.SDCard.."/mydir")
EndIf
User avatar
airsoftsoftwair
Posts: 5626
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Makedir on SD card failed

Post by airsoftsoftwair »

Ok, this is fixed now.

Code: Select all

- Fix [Android]: MakeDirectory() couldn't create directories when using absolute paths
As a workaround, just avoid using absolute paths, e.g.

Code: Select all

ChangeDirectory(t.SDCard)
MakeDirectory("mydir")
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

Recently updated my android phone to Android 12.1 and discovered some of my old apks now had issues with creating dirs on internal storage.
Since the apkcompiler is not updated to HW91 yet I temporary downgraded to HW9.0 and compiled apks with 4.0, but the app failed to create the directories. Then I did a test using HW8.0 and compiler 3.2, and the app worked.

I made an example with some different aproaches to detect and create the dirs and some files.

Code: Select all

@DISPLAY {Title = "Dir test", ScaleMode = #SCALEMODE_AUTO, Orientation = #ORIENTATION_LANDSCAPE, FitScale = True, SmoothScale = True, HideTitleBar = True}
ExitOnError(False)
t = GetSystemInfo()
If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
	filedir$ = "/mnt/sdcard/DIR1"
	TextOut(120, 75, filedir$)
	If Not Exists(filedir$.."/testfile.dat")
		TextOut(100, 50, "MAKING DIR 1")
		MakeDirectory(filedir$)
	EndIf	
	StringToFile("", filedir$.."/testfile.dat")
	
	filedir$ = "/storage/emulated/0/DIR2"
	TextOut(120, 125, filedir$)
	If Not Exists(filedir$.."/testfile.dat")
		TextOut(100, 100, "MAKING DIR 2")
		MakeDirectory(filedir$)
	EndIf	
	StringToFile("", filedir$.."/testfile.dat")
	
	filedir$ = t.SDCard.."/DIR3"
	TextOut(120, 175, filedir$)
	If Not Exists(filedir$.."/testfile.dat")
		TextOut(100, 150, "MAKING DIR 3")
		MakeDirectory(filedir$)
	EndIf	
	StringToFile("", filedir$.."/testfile.dat")
	
	filedir$ = t.SDCard.."/DIR4"
	ChangeDirectory(t.SDCard)
	If Not Exists("DIR4/testfile.dat")
		MakeDirectory("DIR4")
		TextOut(100, 200, "MAKING DIR 4")
	EndIf
	;ChangeDirectory(t.SDCard.."/DIR4")
	StringToFile("", "DIR4/testfile.dat")
EndIf
WaitLeftMouse
If I create the apk with HW9.0 and Compiler 4.0 the app will not create any directories at all. Nor will it detect if the files already exists.
App from HW8.0 and compiler 3.2 does create DIR1 and DIR4, and does detect the files if exists.

If I compile it to applets with HW8.0,HW9.0 and HW9.1 and run them on the phone from Hollywood Player 9.1 all applets does work by creating DIR2, DIR3 and DIR4. But for some reason the applets are not able to create DIR1, nor detect if it already exists. So the player does not go well with the path /mnt/sdcard/
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

To make the difference more clear, I split the combined examples from my previous post into four smaller examples.

example1:

Code: Select all

@DISPLAY {Title = "Dir test", ScaleMode = #SCALEMODE_AUTO, FitScale = True}
ExitOnError(False)
If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
	filedir$ = "/mnt/sdcard/DIR1"
	TextOut(100, 75, filedir$)
	If Exists(filedir$)
		TextOut(100, 100, filedir$.." exsists")
	Else
		TextOut(100, 100, "MAKING DIR 1")
		MakeDirectory(filedir$)
	EndIf	
	If Exists(filedir$.."/testfile.dat")
		TextOut(100, 125, "File in DIR1 exists")
	Else
		StringToFile("", filedir$.."/testfile.dat")
		TextOut(100, 125, "Creating file in DIR1")
	EndIf
EndIf
WaitLeftMouse
example2:

Code: Select all

@DISPLAY {Title = "Dir test", ScaleMode = #SCALEMODE_AUTO, FitScale = True}
ExitOnError(False)
If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
	filedir$ = "/storage/emulated/0/DIR2"
	TextOut(100, 75, filedir$)
	If Exists(filedir$)
		TextOut(100, 100, filedir$.." exsists")
	Else
		TextOut(100, 100, "MAKING DIR 2")
		MakeDirectory(filedir$)
	EndIf	
	If Exists(filedir$.."/testfile.dat")
		TextOut(100, 125, "File in DIR2 exists")
	Else
		TextOut(100, 125, "Creating file in DIR2")
		StringToFile("", filedir$.."/testfile.dat")
	EndIf
EndIf
WaitLeftMouse
example3:

Code: Select all

@DISPLAY {Title = "Dir test", ScaleMode = #SCALEMODE_AUTO, FitScale = True}
ExitOnError(False)
t = GetSystemInfo()
If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
	filedir$ = t.SDCard.."/DIR3"
	TextOut(100, 75, filedir$)
	If Exists(filedir$)
		TextOut(100, 100, filedir$.." exsists")
	Else
		TextOut(100, 100, "MAKING DIR 3")
		MakeDirectory(filedir$)
	EndIf	
	If Exists(filedir$.."/testfile.dat")
		TextOut(100, 125, "File in DIR3 exists")
	Else
		TextOut(100, 125, "Creating file in DIR3")
		StringToFile("", filedir$.."/testfile.dat")
	EndIf
EndIf
WaitLeftMouse
example4:

Code: Select all

@DISPLAY {Title = "Dir test", ScaleMode = #SCALEMODE_AUTO, FitScale = True}
ExitOnError(False)
t = GetSystemInfo()
If PermissionRequest(#PERMREQ_WRITEEXTERNAL)
	filedir$ = t.SDCard.."/DIR4"
	TextOut(100, 75, filedir$)
	ChangeDirectory(t.SDCard)
	If Exists(filedir$)
		TextOut(100, 100, filedir$.." exsists")
	Else
		TextOut(100, 100, "MAKING DIR 4")
		MakeDirectory("DIR4")
	EndIf	
	If Exists(filedir$.."/testfile.dat")
		TextOut(100, 125, "File in DIR4 exists")
	Else
		TextOut(100, 125, "Creating file in DIR4")
		StringToFile("", "DIR4/testfile.dat")
	EndIf
EndIf
WaitLeftMouse
	
Example 1 and Example 4 works well when compiled with HW8.0/Apk-Compiler 3.2
None of the examples work when compiled with HW9.0/Apk-Compiler 4.0
Example 2,3 and 4 works as applets with Hollywood Player 9.1 on android. Example 1 does not work.

My question is then whhat makes the above codes work with HW8/Compiler 3.2 but not with HW9/Compiler 4.0?
And why does not example 1 work with Hollywood Player 9.1?
Post Reply