Okay, so there have been numerous forum posts in the past about getting local HTML files to load in a webview on Android. It’s traditionally been a huge pain because for whatever reason Android wouldn’t read files from the system.ResourceDirectory. I believe that Corona has managed to fix this so that opening local webpages is nice and easy. HOWEVER, when I tried posting a tweet with an image attached on my own Android device, I got an error message saying that it couldn’t find the image I wanted to upload, even though it was sitting right there in my app’s resource directory. This seemed familiar, so I tried using a workaround that I used last year before Corona fixed the whole HTML thing.
The big thing to remember here is that images that are in your system.DocumentsDirectory can be attached to tweets just fine. So if you’re trying to attach an image that you downloaded to system.DocumentsDirectory, or one that you generated within the app via display.capture(), then you shouldn’t have any problems. BUT - if you want to attach an image that resides in your resource directory, you’ve got to copy that file over manually. And to make matters worse, for whatever reason, it doesn’t work with images that have .jpg or .png file extensions. But if you put an image file in your resource directory and change its file extension from .jpg or .png to .txt, then you can copy that file to your documents directory and rename it with the right file extension, and then you can attach it to your tweet! I know it’s a pain, but I tested it using the system below and it worked for me. Here’s how you do it:
First, add a file called copyFile.lua to your project with the following code:
-------------------------------------------------------------------------------- -- CHECK IF A FILE EXISTS -------------------------------------------------------------------------------- local function fileExists(filePath) local exists = false local fileHandle = io.open( filePath, "r" ) if (fileHandle) then -- nil if no file found exists = true io.close(fileHandle) end return(exists) end -------------------------------------------------------------------------------- -- COPY A FILE -------------------------------------------------------------------------------- local function copyFile(source, destination) if not fileExists(source) then return nil end local sourceFile = io.open( source, "r" ) local destinationFile = io.open( destination, "w" ) local fileData = sourceFile:read( "\*a" ) if not fileData then print( "read error!" ) return false else if not destinationFile:write( fileData ) then print( "write error!" ) return false end end sourceFile:close() destinationFile:close() return true end return copyFile
Then require that module into your project like so:
local copyFile = require("copyFile")
Then copy your image file (with the fake “.txt” file extension) to your documents directory like so, giving the copy the right file extension:
copyFile(system.pathForFile("tweetImage.txt", system.ResourceDirectory), system.pathForFile("tweetImage.jpg", system.DocumentsDirectory))
Now that you’ve made copy in your documents directory, which your Android device can see and work with, you can tweet that image out like so:
twitter.tweet("Wow this was a lot of work to tweet this image!", "tweetImage.jpg", callbackListener)
I hope that all makes sense. I know it’s a lot of work, but keep in mind that if you are tweeting images that are already in your system.DocumentsDirectory then none of this stuff needs to happen - you can go ahead and tweet those images easy-peasy. Thanks for your patience, and enjoy!