Category Archives: Code

CoronaSDK: Building and installing on a Kindle Fire

Amazon's Kindle Fire

  1. Build the app using  File > Build > Amazon/Kindle…
  2. On the Fire, go to Settings and tap More
  3. Tap Device
  4. Turn ON “Allow Installation of Applications — from unknown sources”
  5. Plug in a Micro-B usb cable into the Fire and the other into your computer (in this case a MacBook Air)
  6. The Kindle should be showing up on your desktop
  7. Copy the .apk file generated by Corona onto the Kindle (I’m not sure that it makes a difference where — I used the “downloads” folder)
  8. Eject the Kindle from the Mac
  9. On the Kindle you’ll need a File management application installed (I used “ES File Explorer” because it was free and had the most reviews)
  10. Use the File manager to find your app and tap it to install it
  11. Profit!

CoronaSDK: How to know if a file exists?

Update: Dejay was correct, you have to try and open the file to know for sure if it’s there or not.

Want to know if a file exists before you use it?

Lately, I’ve been wanting something a little more dynamic in my projects for opening various Scenes (think Director, but not).   I needed a way of looking to see if a file existed or not before I tried to load it up.  Here’s the result:

function fileExists(fileName, base)
  assert(fileName, "fileName is missing")
  local base = base or system.ResourceDirectory
  local filePath = system.pathForFile( fileName, base )
  local exists = false
 
  if (filePath) then -- file may exist. won't know until you open it
    local fileHandle = io.open( filePath, "r" )
    if (fileHandle) then -- nil if no file found
      exists = true
      io.close(fileHandle)
    end
  end
 
  return(exists)
end

Typical usage:

if fileExists("myGame.lua") then
  -- do something wonderful
end

If the file does not exist, the CoronaSDK library function system.pathForFile() returns nil.  It also returns a Warning in the console, which can be safely ignored.

By default it checks your app’s asset directory (system.ResourceDirectory), however, if you’d like to check if a file you created exists you can pass in an alternative base path like so:

if fileExists("some_file.txt", system.DocumentsDirectory) then
  -- do something wonderful
end

The three valid options for base path are:

  1. system.ResourceDirectory
  2. system.DocumentsDirectory
  3. system.TemporaryDirectory