"the application has been corrupted" error still occurring

With the recent tut on i/o, I decided to try something that I’ve wanted to do——Read content from an HTML file; replace whatever is between the and tags with new content (i.e., from another file); and then display the resulting rendered HTML into a webPopUp (can’t use webView b/c it needs to work on Android). Unfortunately, I am getting the infamous “the application has been corrupted” error.

I have read through the many past posts with this same problem and have attempted the many solutions. I have

  • checked the capitalization on my file names and in my code,
  • moved everything into the base ResourceDirectory,
  • renamed my files with a .txt extension.
  • created (for good measure) the file to be read into the webPopUp on the fly (unfortunately, this has to be an .html file as that’s what the webPopUp reads)
  • copied the files into the actual sandbox folder for this app
  • I created a “Documents” folder in the main resource directory.
    But all to no avail. I’m not sure how to get this working on Android. It seems as though it should be simple enough. Any ideas? (see my complete code below).

[lua]-- main.lua
– PURPOSE: Test for pattern matching (so that I can eventually replace what occurs between two HTML tags)

_H = display.contentHeight
_W = display.contentWidth
–read in the text from a file
local path1 = system.pathForFile(“container.txt”, system.DocumentsDirectory)
local file = io.open(path1,“r”)
local oldText = file:read("*a")
io.close(file)

–now get the data you’ll use to replace this text
local path2 = system.pathForFile(“content.txt”,system.DocumentsDirectory)
local file2 = io.open(path2,“r”)
local newText = file2:read("*a")
io.close(file2)
file2 = nil

–now try replacing everything between those two indexes
local changedText = string.gsub(oldText, “([%c%w%s%p%d]+)”, “”…newText…"")
–now write the data to a new file
local path3 = system.pathForFile(“newFile.html”,system.DocumentsDirectory)
file = io.open(path3,“w”)
file:write(changedText)
io.close(file)
file = nil

–now let’s load this file into a web popup (can’t use native.webView b/c it only works on iOS devices currently, but look to do it when they come to Android)
local options = {hasBackground = false, baseUrl = system.DocumentsDirectory}
local text = native.showWebPopup(50, 50, _W*.5,_H*.9,“newFile.html”, options)[/lua] [import]uid: 19999 topic_id: 21944 reply_id: 321944[/import]

You talk about moving the files into the ‘resource’ directory and then use the ‘documents’ directory in your code. You could be confused with the directory structure.

You might want to read this blog from Jonathan Beebe about reading and writing files in Corona.
http://blog.anscamobile.com/2012/02/reading-and-writing-files-in-corona/

Hope this helps…
Jeff
[import]uid: 14119 topic_id: 21944 reply_id: 87300[/import]

Jeff,
Thanks for the link, but that tutorial is what I was referring to in my opening statement on “With the recent tut on i/o…”. The “ResourceDirectory” is the directory in which the main.lua file resides. Supposedly, i/o can only read (not write) documents in that folder. The “DocumentsDirectory” is a folder that is created in a sandbox when CoronaSDK launches the application. You can find it by going to File > Show Project Sandbox. I’m not sure if that’s the right way to access that directory, but that’s how I’ve gotten files referencing that directory to work properly in the simulator.

Consequently, I’ve also created my own “Documents” directory in my main directory, but that doesn’t work, either. No matter where I put these files, I can get them to work in the simulator, but not on the actual Android device. Any ideas?

Peter- [import]uid: 19999 topic_id: 21944 reply_id: 87356[/import]

If I understand you right, your placing files in the simulator sandbox and expecting them to be moved to the device sandbox after an install. That won’t happen. The app documents directory will be empty after an install until your app writes something there.
What you need to do is either download the file(s) to the documents directory or copy the files from the resource directory to the documents directory.
If you need help doing any of that, let me know.

Jeff
[import]uid: 14119 topic_id: 21944 reply_id: 87378[/import]

Jeff,
THANK YOU! That clarification is extremely important and is neither mentioned nor suggested anywhere in the documentation or the tutorials on the topic. Given this info, I decided to create a script that:

  1. checks if the file already exists in the Documents Directory

  2. if not, then copy it from the ResourceDirectory to the DocumentDirectory

  3. once all files are in place, use the files as desired (both writing and read them).

Once I did this, I was very excited to see it work in the simulator (the whole file checking and creation process). I then built it for my Android phone, crossed my fingers, and still got the same error. _ Any ideas on what might be causing this error? _

BTW, here’s the code for checking if a file already exists in the Documents Directory and, if not, copying it from the ResourceDirectory.
[lua]local function checkForFile (file)
local checkPath = system.pathForFile(file,system.DocumentsDirectory)
local isFileThere = io.open(checkPath,“r”)
if isFileThere == nil then
–file doesn’t exist, so copy file from base directory to target Directory)
local existingPath = system.pathForFile(file,system.ResourceDirectory)
print (existingPath)
if existingPath == nil then
print (“ERROR: There is no “…file…” in the Resource Directory”)
else – file exists, so copy it from the Resource Directory to the Documents directory
local readFile = io.open(existingPath,“r”)
fileContents = readFile:read("*a")
io.close(readFile)
readFile = nil
path = nil
–now copy to new directory
local newPath = system.pathForFile(file,system.DocumentsDirectory)
local newFile = io.open(newPath,“w”)
newFile:write(fileContents)
io.close(newFile)
newFile = nil
path2 = nil
print (file…" copied successfully")
end
else --file already exists
print(file…" already exists in the Documents Directory")
end
end[/lua] [import]uid: 19999 topic_id: 21944 reply_id: 87556[/import]

GOT IT!
I missed one thing when creating my fix above. I forgot that there are problems reading .html files from the ResourceDirectory. I changed those two files to .txt files and copied them to the DocumentsDirectory and then, when I built it for Android, it worked.

Thanks for your help and clarifications on the whole file system directory thing, Jeff.

-Peter- [import]uid: 19999 topic_id: 21944 reply_id: 87564[/import]

Glad I could help.

Jeff
[import]uid: 14119 topic_id: 21944 reply_id: 87595[/import]