Cant find file in Ressoure Directory!! (only Android)

Hi,

I try to open several file that are in my root folder (where my main.lua is)

index.html
index.css
backup.png
it works fine on iPhone (device) but not on Android!

on Android I only can open the
index.css all other are not assign/found correctly.

   
 file = 'index.html"  
 local base = system.ResourceDirectory   
 path = system.pathForFile(file, base )  
 if path then   
 print ("in path")  
 served = io.open(path,"r")   
 end  
  

For the index.html for example I do get
path: file:///android_asset/index.html
and a NIL as result for -> served = io.open(path,“r”)

the index.css gives a
path: /data/data/com.chris.mydayscorona/files/coronaResources/index.css
at io.open does work fine.

the backup.png gives a
path: backup.png
and io.open does NOT find the file.

i also tried manually to set the path same as index.css so for example
/data/data/com.chris.mydayscorona/files/coronaResources/index.html
but io.open does NOT find a file.

I really need to have that fixed soon.

thx
chris

thx
chris
[import]uid: 4795 topic_id: 19090 reply_id: 319090[/import]

Here another example, please let me know how to do it right

the app itself is just a folder with:

aaa.css
aaa.htm
aaa.html
aaa.jpg
aaa.png
main.lua

----main.lua:

mytext=native.newTextBox( 0, 0, 320, 480 )  
mytext.size = 8  
result = ""  
   
function tryopen(whatfile)  
 local base = system.ResourceDirectory   
 result = result .. "\n----\nsearch for path:"  
 path = system.pathForFile(whatfile, base )  
 if path then   
 result = result .. "\nin path: "..path  
 served = io.open(path,"r")   
 if served ~= nil then  
 result = result .. "\nYEAH! opened"  
 else  
 result = result .. "\nARGG! -----\> Path OK, but not opened !"  
 end  
 else  
 result = result .. "\nSNIEF! NO PATH!?"  
 end  
   
end   
   
 tryopen ("aaa.html")  
 tryopen ("aaa.css")  
 tryopen ("aaa.htm")  
 tryopen ("aaa.html")  
 tryopen ("aaa.png")  
   
mytext.text = result   

only the aaa.css is opened right, all other get paths from filetopath, but io.open can’t open it.!

For your easy testing I prepared:
ZIP File with its source & compiled.
chris
[import]uid: 4795 topic_id: 19090 reply_id: 73945[/import]

Hey Chris have you filed this as a bug as well? http://developer.anscamobile.com/content/bug-submission

Peach :slight_smile: [import]uid: 52491 topic_id: 19090 reply_id: 74114[/import]

Chris,

Here is an explanation. All of the files in your Resource directory are really inside of your APK file. Your APK file is really a zip file. The io.open() function is not able to extract files from a zip file. This is why io.open() cannot access your resource files. This is a limitation imposed by Android, not Corona, that we’re all stuck with at the time being. Now, iOS does not have this problem because an iOS app bundle is really its own file system whose files are accessible via C/C++ file functions. That is, an iOS app bundle is not a compressed zip file like it is on Android.

Now that said, we have implemented some hack-ish solutions. We actually extract certain file types for you and copy them to a temp directory when you call system.pathForFile(), but we only do that for very particular file extensions. Files such as MP3 and TXT are automatically extracted. Files such PNG and JPG and HTML are not. The system.pathForFile() function will then return the path of the file copy that is in the temp directory instead of the path to where it is in the resources directory. It’s an admittedly hack-ish solution to get certain files loaded into the C++ side of Corona. The reason we don’t automatically extract HTML files is because they typically depend on image files to be in the same directory, files which we should NEVER automatically extract because they would waste too much storage space. So, it’s a tough problem that we don’t have a good solution for yet.

Bottom line, and I’m sorry to say, you’re going to have to redesign your app to work-around this Android limitation. There’s nothing for Ansca to fix on our end… other than to find a better work-around solution.

I had a much more long winded post somewhere on the forums about this topic that I should really point you too. I’ll try to send a link to it sometime soon. It contains much more details than what I’ve mentioned above. [import]uid: 32256 topic_id: 19090 reply_id: 74157[/import]

Just for interest,

why can you extract mp3, txt, css and not html, png …
that makes no sense for me! can’t you just add that extensions into your
‘unzip’ function?
there could be several options from YOUR side.

we could give all Ressource Files a specific Name… like
res_index.html
res_image.png
so YOU know, that its a resource file and the user wish to extract it.

Also does you extract while installation… or on the fly when
pathtofile is called? Than it would even not problem to extract whatever the user
wants… its his responsibility.

please don’t make restrictions for something, while the user (coder) just need to
take bit care. To be unable storing Image Files in the Ressource Folder is quiet
a heavy thing !!!

So is there a list of filetypes that you can unpack… just mp3 and txt (i saw also css)

please, improve that and yes I am interested in that other Post you may find the link.

Thanks
Chris
[import]uid: 4795 topic_id: 19090 reply_id: 74161[/import]

I helped myself right now by renaming all files like

index_html.mp3
index_css.mp3
index_png.mp3

so all file extensions are mp3.

Than I can open all that files just fine :slight_smile:

couldn’t YOU do some file renaming hack?

greets
chris
[import]uid: 4795 topic_id: 19090 reply_id: 74172[/import]

Chris,

Having system.pathForFile() automatically extract all files is not an acceptable solution either, because it would near double the amount of storage space required on an Android device. For example, if the APK file contains 100 MB of asset files then it would automatically bloat the storage space used to ~200 MB. We could never get away with that on our end. The real solution is to find a way to access these files without automatically extracting them from within the APK. Something we haven’t decided on from our end yet (there’s been much debate).

Let’s take a step back. What are you trying to accomplish? Perhaps I can suggest an alternative solution that will work on all platforms.
[import]uid: 32256 topic_id: 19090 reply_id: 74250[/import]

@Joshua
Could you, please, write all file extensions we may open via system.pathForFile()/io.open? How about XML?
Thanks [import]uid: 9058 topic_id: 19090 reply_id: 74273[/import]

Please see the link below for a more long winded post about this topic. It lists the file extensions that has this “special handling” that I mentioned above…

http://developer.anscamobile.com/forum/2011/09/25/application-has-been-corrupted-error#comment-65109
[import]uid: 32256 topic_id: 19090 reply_id: 74288[/import]

is that extracting not happening by runtime?

anyhow… i solved it like that.

i just named all my files like

index_html.mp3 --original index.html
bottom_png.mp3 --original bottom.png
you see, in that case I can access them all.
Now when opened I just take the content and do something with it.

in that case I send it through Wifi to a Client :slight_smile:

thx
chris
[import]uid: 4795 topic_id: 19090 reply_id: 74290[/import]

If you want, you can rename your files like this…

index._html
bottom._png

…because the extension I listed on that other post are the only extensions that do not get automatically extracted. That is, all extensions that are not in that list are automatically extracted. So feel free to invent your own extensions that better match the names of the files like I did up above.

To me, it sounds like what you really need is the ability to “copy” resource files so that you can modify them later. Is that right? In which case, perhaps the best solution is for us to add a copy() function to Corona which will automatically extract files within the APK… and any file you want. [import]uid: 32256 topic_id: 19090 reply_id: 74292[/import]

I have a similar problem. I understand the workaround, and I guess I’ll have to go that route for now. It’s kind of a pain, because it means I can’t just copy the files into the resource directory and preview/interact with them the way I do now (on iOS).

In my case I have a Resource directory called “episodes” that contains a bunch of .json and .jpg files. When the user runs the app for the first time, I copy all of these files to the Documents directory to initialize their game episodes (I do this because the episodes are all editable, and they can add more, which of course have to be in Documents).

I’m kind of annoyed that Corona has internal access to these files, but doesn’t give developers the same access, other than via this hack. It seems to me that doing the extraction at the time of the pathForFile call would be pretty easy and work fine. For example, if someone does this:

 system.pathForFile("foo.jpg", system.ResourceDirectory)  

Then the very next thing they are going to do is try to open the file at the path returned. This seems to me like it would be an excellent time to just copy/extract the resource file to a temp location and return a path to that temp location. And then everything would work as you would expect (with no filename hackaround required, and no include/exclude processing as you do now, not to mention no unnecessary expansion of files that aren’t accessed at runtime from user code). Am I missing something here?

If you can’t solve this properly because extracting at runtime is too hard, then at least in my case, I would like a hack to support extracting Resource sub-directories by name. I’d be fine with calling the subdirectory “_xxxx” or “user_xxxx” or “runtime_xxxxx” or whatever to indicate that I want to access these files from my application code. At least that way the files in the directory would still all be intact and easier to deal with.
[import]uid: 111481 topic_id: 19090 reply_id: 86703[/import]

Hmm. OK, I re-read the other thread on this topic and it explains that system.pathForFile does actually dynamically extract files.

http://developer.anscamobile.com/forum/2011/09/25/application-has-been-corrupted-error#comment-65109

So why doesn’t it extract any file that I ask for again? I assume that API is only called from user code, and if user code asks for a file, then they probably want the file (whatever type it is).

If it’s really just the “html files probably won’t work if they rely on resources and we don’t want to auto-extract them all” then I’d rather you just document that that isn’t supported and let the rest of us have at our files! I mean, if the user really cared, they could just call pathForFile on all the files needed by the html file and it would work fine (assuming you just fixed pathForFile to return any file the user asked for). I just don’t quite understand what you’re trying to protect people from, and why that’s more important that allowing basic access to Resource files for people who know what they’re doing.
[import]uid: 111481 topic_id: 19090 reply_id: 86705[/import]

OK, I changed all my .jpg files to .jpeg files and it works fine now (these files are all created in Corona, and Corona seems to be fine with the .jpeg extension on load).

[import]uid: 111481 topic_id: 19090 reply_id: 86708[/import]