External Storage

Thanks for the clarification. I appreciate it.

Hi Scott,

I studied your post carefully, is my following understanding correct?

externalStorage.listFiles() - gives you a list of files on the device itself (has nothing to do with an SD card)

externalStorage.listFiles(externalStorage.music) - gives you a list of files that are in the device’s /Music folder (has nothing to do with an SD card)

externalStorage.listFiles(externalStorage.sdCardPath()) - gives you a list of files and folders that are in the root folder (starting folder) of the SD card

externalStorage.listFiles(externalStorage.sdCardPath()…externalStorage.music) - gives you a list of files in the /Music folder of the SD card (if a ‘Music’ folder exists on the SD card)

Thanks

Yes

Great, thanks.

Hi, I’m still trying to get this to work for an app…  I’m finding that these 2 function calls give the exact same output (a list of files on the device):

externalStorage.listFiles()

externalStorage.listFiles(externalStorage.sdCardPath())

even though that’s what we have outlined in the post above.dated 06 March 2017 - 12:32 PM

Is that a bug, or does externalStorage.sdCardPath() have to be used differently? How do we get a list of files that are on the SD card?  print(externalStorage.isSdCardConnected()) is coming back as true

Also is externalStorage.sdCardPath() supposed to return a string?  I’m getting the following error when I run :print(json.encode(externalStorage.listFiles(externalStorage.sdCardPath()…’/Music’))) 

 

ERROR: Runtime error

?:0: attempt to concatenate a function value

stack traceback:

     ?: in main chunk

Let me know if I’m doing something wrong.

Thanks again

I am looking into this

here is RSS feed of the update logs

https://bitbucket.org/coronalabs/store-hosted-externalstorage/rss?token=829a4286184c5ab3c5c1b99a52d25ab9

This should be fixed, please note you must add true at the end of functions

example

print( json.encode( externalStorage.listFiles(externalStorage.sdCardPath(),true)))

please visit https://scotth.tech/plugin-externalStorage to see changes

Thanks so much Scott for the quick fixes!

One last thought, I assume there"s no easy way to get a list of files *recursively* on the plugin side, right? Basically I need to be able to get a list of all files a user might have in folders or subfolders (with paths) . I could then with lua see which of those found filenames end with .wav and allow users to select it. Kind of like how the Android app “Installer - Installer APK” scans your entire phone (folders and subfolders) looking for any .apk files.

Thank you again for the great plugin!

Glad you like, to answers your other question. There is no magic( by that I mean easy) way to do this. You are probably better off trying to spin your own solution. This plugin gives you basically give you raw power to the phones file system. As fun as making a demo sounds, I cannot do everything( even how much I try ). I got another bug to deal with. Thanks,

Scott

ok thanks, I’ll give it a shot…

Hi Scott,

Thanks for this plugging too.

I have purchased it some time ago, and, I have a little question:

Is there any way that I can create a folder on the external storage?

The idea is that, my app, needs this plugging to perform some data backup, and don’t want to keep the files hanging about along the external storage root. I’d love to create my own folder with my app’s name, and then, save the backup files into that folder.

Is there any way to do it?

Thanks again in advance,

P.D. I have adjust my SD card as extension for the internal memory, so, the function:

externalStorage.isSdCardConnected()

gives me false, is correct isn’t it?

Thanks again

I am adding folder api, yes, I don’t use android a lot so I cannot answer the sd card extension question

added folder api

externalStorage.makeFolder("/Music/NewFolder")

Dear Scott,

Thanks for your quick answer and your quick solution,

But, I’m unable to do it to work …

externalStorage.makeFolder("/Music/NewFolder")

give me an error: attempt to call field ‘makeFolder’ a nil value.

Perhaps the update you have performed is already not uploaded to the corona repository ?

By other hand,

Dear, we can create folders using the LFS as follow:

local temp_path = externalStorage.sdCardPath()

if temp_path and type (temp_path) == ‘string’ then

  – Change current working directory

  local success = lfs.chdir( temp_path )  --returns true on success

  local new_folder_path

  if ( success ) then

  lfs.mkdir( “NewFolder” )

     new_folder_path = lfs.currentdir() … “/NewFolder”

  print (“LFS SUCCESS”)

  end

end

 print (“Folder Created”)

end

But for this, we need any chance to get the root to the sdCardPath.

externalStorage.sdCardPath() don’t return an string containing the real path, but, a function.

Is there any way to get the correct string to the SDPath to use LFS to manipulate the files?

I know the most difficult in your plugging is to correctly obtain the path to the sdCard, due, different Android version, comes with different paths for the external SD (sdcard, sdcard1, mnt/sdCard… ) and I really loves the way you have performed to solve it, and for that I bought your plugging. But do you think it is possible your plugging gives us the correct path for the sd as mentioned in string format to use it with LFS ?

If not, can you arrange a correct sample, to simple do the follow?

create a Folder named ‘myFolder’ at the root of the SDCard.

copy a file from system.documentsDirectory to  that folder.

list the total files inside that folder (list files at SDCard/myFolder… to check if my file is there. Also can get the timestamp for that file.

delete certain file from that folder at SDCard.

copy a file from that folder (SDCard/myFolder) to the system.documentsDirectory.

As you can see, all these can be solved if you plugging simple gives or return the correct path to the SDCard in string, due, we can use LFS to manipulate all these requests.

Thanks again for your great job.

My plugin is not designed to work with the lfs. I build my own solution for handling external storage. externalStorage.makeFolder("/Music/NewFolder") works I just tested it. It was not added to the lua stubs on mac or windows so I just fixed that. It should work if you ignore the error and test on device, but it should not show an error in simulator within the hour. If you want to access the sd card you should use the build in functions. This plugin allows you to transfer files from your app sandbox to android file system and vise versa.

print( json.encode( externalStorage.listFiles(externalStorage.sdCardPath(),true)))

local pathCorona = system.pathForFile( "test.txt" ) print(externalStorage.copyFile(pathCorona, externalStorage.sdCardPath().."hello123.txt", true))

print( externalStorage.deleteFile(externalStorage.sdCardPath().."test.txt") )

please read documentation

Hi again Scott,

Thanks for your answer.

Dear, now it works, but, please, note the follow:

1. print( externalStorage.sdCardPath() )  --> “/storage/sdcard1/” --> PERFECT!!

2. externalStorage.makeFolder("/NewFolder")  --> creates the folder at the internal memory root --> PERFECT!!

But,

3. externalStorage.makeFolder(externalStorage.sdCardPath() … “/NewFolder”)  --> creates the folder at internal memory/storage/sdcard1 not at the external SD --> FAIL!!

I think, by default, externalStorage.makeFolder() assume the root of the internal memory and then concatenate the value you pass.

Really, the folder is created at:

/storage/emulated/0/storage/sdcard1/NewFolder

But ought to be at:

/storage/sdcard1/NewFolder

So, by any reason, the makeFolder function always concatenate the argument (the name of the folder) to the internal root path.

I can see the externalStorage.directory() return the internal root path (/storage/emulated/0 in my case)

can you revise this, and avoid to always concatenate the internal path to all the arguments passed to the makeFolder() ?

If you avoid to concatenate it, we can use the follow:

externalStorage.makeFolder( externalStorage.directory() … “NewFolder” --> to create at internal storage

externalStorage.makeFolder( externalStorage.sdCardPath() … “NewFolder” --> to create at the SD Card Root

Or arrange another way to create folders at external storage.

Thanks again for your great job,

Try externalStorage.makeFolder(externalStorage.sdCardPath() … “/NewFolder”, true)

Hi Scott,

externalStorage.makeFolder(externalStorage.sdCardPath() … “/NewFolder”, true)  --> appear to don’t do anything

externalStorage.makeFolder(externalStorage.sdCardPath() … “/NewFolder”, false)  --> create a folder at internal memory, named storage, and inside it, other named sdcard1, and inside this last, the NewFolder, so it is the very same

I think the true field is something similar to ‘not concatenate the root for the internal memory’ isn’t it? but, appear to not work properly.

Thanks again dear,

sorry try externalStorage.makeFolder(externalStorage.sdCardPath() … “NewFolder”, true)

Dear Scott,

sorry for delay in answer. I have limited time and today have tested your suggestions.

Dear,

The following DONT WORK AT ALL AND DON’T DO ANYTHING

externalStorage.makeFolder(externalStorage.sdCardPath() … “NewFolder”, true) --Do nothing

externalStorage.makeFolder(externalStorage.sdCardPath() … “/NewFolder”, true)  --Do nothing

externalStorage.makeFolder (externalStorage.sdCardPath() … “NewFolder2/”) --Do nothing

Neither of the above do anything. Don’t work at all.

The following WORKS but not in a correct way, as explained for each one:

externalStorage.makeFolder(externalStorage.sdCardPath() … “/NewFolder”, false) --> create a folder in internal memory, at a folder named storage/sdcard1/NewFolder

Could you revise it and let me know if there is any way to create and copy files to the external SD card ?

I continue thinking your routine makeFolder use the internal memory root path adding it to all the passes arguments. Any chance to not add this internal root path to the folder? I think you prepared the true second argument to do this, but, as mentioned, don’t works dear.

I’m testing in a Android 5.0.2 with an external SDCard attached.

externalStorage.sdCardPath() --> gives me the following :      /storage/sdcard1/

externalStorage.isSdCardConnected() --> give me : TRUE

Could you revise this ? I need to create a folder to the external SD Card, and copy files there. It is to backup info. Internal memory is not the correct place to store the backup. Need to write to the external SD Card (externalStorage.sdCardPath)

Thanks again,