Makedir on SD card failed

Discuss any general programming issues here
xabierpayet
Posts: 283
Joined: Fri Feb 24, 2012 9:34 am

Re: Makedir on SD card failed

Post by xabierpayet »

i cannot query the units, i can write directly to the SDCard but i need to know the correct label of the card used ever, for my tests is enough, but a better solution ever will be nice
amyren
Posts: 428
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

I think I might have found a solution/workaround
According to this page https://developer.android.com/training/ ... oogle-play the folder /Android/media/ is a part of the shared storage.
I found I could successfully create a folder with any chosen name in the /Android/media/ folder, and this works if using the path /storage/emulated/0/Android/media/ or simply /sdcard/Android/media/
The folder gets created, but I could not manage to create a file inside it, so that seemed quite pointless so far.

But if the directory name is set to the exact same as the name of the compiled Package, then it works and you can create files or copy files into the directory. And the good thing is that the files will be accessable (opposed to the files in the /Android/data/ folders which can not be accessed)

Here is a working example:

Code: Select all

@DISPLAY {Title = "Dir test", ScaleMode = #SCALEMODE_AUTO, FitScale = True}
ExitOnError(False)
	filedir$ = "/sdcard/Android/media/com.amy66dev.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("test", filedir$.."/testfile.dat")
		TextOut(100, 125, "Creating file in owndir")
	EndIf
	If Exists("testfile2.dat")
		TextOut(100, 150, "File in own dir exists")
	Else
		StringToFile("test", "testfile2.dat")
		TextOut(100, 150, "Creating file in owndir")
		CopyFile("testfile2.dat", filedir$)
		TextOut(100, 175, "Copying file from owndir")
	EndIf
WaitLeftMouse
amyren
Posts: 428
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

After my last post on this matter I found a slightly different method to determinate the shared storage folder.

One could set the filedir$ variable to the shared storage folder location area like this

Code: Select all

ta = GetSystemInfo()
filedir$ = ta.InternalStorage.."/testdir"
MakeDirectory(filedir$)
But I realize I've been overcomplicating things here, since a simple

Code: Select all

filedir$ = "testdir"
MakeDirectory(filedir$)
would do the same thing, since android apear to operate in this area by default anyway.

The actual issue of accessing the external SDCARD remains unresolved
amyren
Posts: 428
Joined: Thu May 02, 2019 11:53 am

Re: Makedir on SD card failed

Post by amyren »

I think I have found a better solution now that enables handling files on internal and external storage.
However, I feel that it is more related to APK Compiler than general programming, so I will create a new thread on the matter in the APK Compiler section.
User avatar
airsoftsoftwair
Posts: 5886
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Makedir on SD card failed

Post by airsoftsoftwair »

Code: Select all

- New [Android]: Added support for the Storage Access Framework (SAF); all Hollywood commands dealing with
  files and directories now also support special files/directories managed by the SAF; this opens up lots
  of new possibilities because SAF can do quite some magic since it doesn't only allow you to read/write
  files on your device but also directly in the cloud like on Google Drive; so Hollywood's support of SAF
  makes all this accessible to Hollywood scripts now without having to write new code; nevertheless, there
  are a few important things to be kept in mind; firstly, SAF path specifications aren't like traditional
  paths which is why you can't parse them using functions like FilePart() and PathPart(); moreover, when
  writing a new file using SAF you can't just call OpenFile() but you have to call CreateFile() first to
  actually create the file; after that you can write data to the file; the same is true for directories:
  you cannot create them using MakeDirectory() but you must use the new CreateDirectory() function first
  (see above); otherwise SAF files and directories will behave pretty much the same as normal files; NB:
  SetFileAttributes() isn't supported for SAF files because for security reasons SAF doesn't support
  changing file dates programmatically; also, MoveFile() isn't supported and CopyFile() and DeleteFile()
  can only be used on single files; to delete a SAF directory using DeleteFile(), you first have to make
  sure it's empty
- New [Android]: PermissionRequest() supports the new flag #PERMREQ_MANAGEEXTERNAL now; this allows your
  app to request the "manage all files on a storage device" permission which will grant your app access
  to all files on the SD card; this can be a useful substitute for the permissions #PERMREQ_READEXTERNAL
  and #PERMREQ_WRITEEXTERNAL which are no longer supported by newer Android versions but keep in mind that
  your app probably won't be allowed on the Play Store if you use #PERMREQ_MANAGEEXTERNAL permission since
  Google is very restrictive about this permission; for apps for your personal use or distribution outside
  the Play Store however using the #PERMREQ_MANAGEEXTERNAL permission is the easiest and fastest way to
  access files on the Android device; if you plan to publish your app on the Play Store and you need to
  access external files, you should use Android's Storage Access Framework (SAF) or MediaStore instead
  (see above for details)
Post Reply