Posting saved image from Documents directory to facebook

I am trying to post an image to my facebook feed from a file saved via the phone’s camera but cannot.

This is taken from the facebook example provided with the SDK.

I updated the post photo function as follows

-- This code posts a photo image to your Facebook Wall... I updated this code to look at the file I put into the Documents directory after I take the pic from the Camera.  
--  
if fbCommand == POST\_PHOTO then  
local attachment = {  
message = "Test Image.",  
source = { baseDir=system.DocumentsDirectory, filename="test.jpg", type="image" }  
 }  

I have validated that test.jpg does indeed exist in the Documents directory after I save it from the camera. If I put a file called test.jpg in my Resource directory and change the above code to reflect that directory it works when posting the image.

[import]uid: 107466 topic_id: 22486 reply_id: 322486[/import]

Take a look at this blog post, very detailed stuff; http://blog.anscamobile.com/2011/12/uploading-photos-to-facebook-in-corona/

Peach :slight_smile: [import]uid: 52491 topic_id: 22486 reply_id: 89818[/import]

Peach,
Thanks for the response :slight_smile:

I actually have used that very same code to test and posting from the Resources directory works but an image saved in the Documents directory does not. I put a comment on that blog post as well.

I am using the code from the Facebook example and an additional piece of code.

--   
-- Project: Facebook Connect sample app  
--  
-- Date: December 24, 2010  
--  
-- Version: 1.3  
--  
-- File name: main.lua  
--  
-- Author: Ansca Mobile  
--  
-- Abstract: Presents the Facebook Connect login dialog, and then posts to the user's stream  
-- (Also demonstrates the use of external libraries.)  
--  
-- Demonstrates: webPopup, network, Facebook library  
--  
-- File dependencies: ui.lua, facebook.lua, json.lua libraries (included)  
--  
-- Target devices: Simulator and Device  
--  
-- Limitations: Requires internet access; no error checking if connection fails  
--  
-- Update History:  
-- v1.1 Layout adapted for Android/iPad/iPhone4  
-- v1.2 Modified for new Facebook Connect API (from build #243)  
-- v1.3 Added buttons to: Post Message, Post Photo, Show Dialog, Logout  
-- v1.4 Added ...{"publish\_stream"} .. permissions setting to facebook.login() calls.  
  
--  
-- Comments:  
-- Requires API key and application secret key from Facebook. To begin, log into your Facebook  
-- account and add the "Developer" application, from which you can create additional apps.  
--  
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license  
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.  
--  
---------------------------------------------------------------------------------------  
  
-- Comment out the next line when through debugging your app.  
io.output():setvbuf('no') -- \*\*debug: disable output buffering for Xcode Console \*\*tjn  
  
-- Load external library (should be in the same folder as main.lua)  
local ui = require("ui")  
local facebook = require("facebook")  
local json = require("json")  
  
display.setStatusBar( display.HiddenStatusBar )  
  
-- Facebook Commands  
local fbCommand -- forward reference  
local LOGOUT = 1  
local SHOW\_DIALOG = 2  
local POST\_MSG = 3  
local POST\_PHOTO = 4  
local GET\_USER\_INFO = 5  
local GET\_PLATFORM\_INFO = 6  
  
-- Layout Locations  
local ButtonOrigY = 175  
local ButtonYOffset = 45  
local StatusMessageY = 420 -- position of status message  
  
local background = display.newImage( "facebook\_bkg.png", true ) -- flag overrides large image downscaling  
background.x = display.contentWidth / 2  
background.y = display.contentHeight / 2  
  
-- This function is useful for debugging problems with using FB Connect's web api,  
-- e.g. you passed bad parameters to the web api and get a response table back  
local function printTable( t, label, level )  
 if label then print( label ) end  
 level = level or 1  
  
 if t then  
 for k,v in pairs( t ) do  
 local prefix = ""  
 for i=1,level do  
 prefix = prefix .. "\t"  
 end  
  
 print( prefix .. "[" .. tostring(k) .. "] = " .. tostring(v) )  
 if type( v ) == "table" then  
 print( prefix .. "{" )  
 printTable( v, nil, level + 1 )  
 print( prefix .. "}" )  
 end  
 end  
 end  
end  
  
local function createStatusMessage( message, x, y )  
 -- Show text, using default bold font of device (Helvetica on iPhone)  
 local textObject = display.newText( message, 0, 0, native.systemFontBold, 24 )  
 textObject:setTextColor( 255,255,255 )  
  
 -- A trick to get text to be centered  
 local group = display.newGroup()  
 group.x = x  
 group.y = y  
 group:insert( textObject, true )  
  
 -- Insert rounded rect behind textObject  
 local r = 10  
 local roundedRect = display.newRoundedRect( 0, 0, textObject.contentWidth + 2\*r, textObject.contentHeight + 2\*r, r )  
 roundedRect:setFillColor( 55, 55, 55, 190 )  
 group:insert( 1, roundedRect, true )  
  
 group.textObject = textObject  
 return group  
end  
  
local statusMessage = createStatusMessage( " Not connected ", 0.5\*display.contentWidth, StatusMessageY )  
  
-- New Facebook Connection listener  
--  
local function listener( event )  
  
--- Debug Event parameters printout --------------------------------------------------  
--- Prints Events received up to 20 characters. Prints "..." and total count if longer  
---  
 print( "Facebook Listener events:" )  
  
 local maxStr = 20 -- set maximum string length  
 local endStr  
  
 for k,v in pairs( event ) do  
 local valueString = tostring(v)  
 if string.len(valueString) \> maxStr then  
 endStr = " ... #" .. tostring(string.len(valueString)) .. ")"  
 else  
 endStr = ")"  
 end  
 print( " " .. tostring( k ) .. "(" .. tostring( string.sub(valueString, 1, maxStr ) ) .. endStr )  
 end  
--- End of debug Event routine -------------------------------------------------------  
  
 print( "event.name", event.name ) -- "fbconnect"  
 print( "event.type:", event.type ) -- type is either "session" or "request" or "dialog"  
 print( "isError: " .. tostring( event.isError ) )  
 print( "didComplete: " .. tostring( event.didComplete) )  
-----------------------------------------------------------------------------------------  
 -- After a successful login event, send the FB command  
 -- Note: If the app is already logged in, we will still get a "login" phase  
 --  
 if ( "session" == event.type ) then  
 -- event.phase is one of: "login", "loginFailed", "loginCancelled", "logout"  
 statusMessage.textObject.text = event.phase -- tjn Added  
  
 print( "Session Status: " .. event.phase )  
  
 if event.phase ~= "login" then  
 -- Exit if login error  
 return  
 end  
  
 -- The following displays a Facebook dialog box for posting to your Facebook Wall  
 if fbCommand == tag\_friend then  
 facebook.showDialog( {action="stream.publish"} )  
 end  
  
 -- Request the Platform information (FB information)  
 if fbCommand == GET\_PLATFORM\_INFO then  
 facebook.request( "platform" ) -- \*\*tjn Displays info about Facebook platform  
 end  
  
 -- Request the current logged in user's info  
 if fbCommand == GET\_USER\_INFO then  
 facebook.request( "me" )  
-- facebook.request( "me/friends" ) -- Alternate request  
 end  
  
 -- This code posts a photo image to your Facebook Wall... I updated this code to look at the file I put into the Documents directory after I take the pic from the Camera in the function at the bottom of the page.  
 --  
 if fbCommand == POST\_PHOTO then  
 local attachment = {  
 message = "LOL.",  
 source = { baseDir=system.DocumentsDirectory, filename="test.jpg", type="image" }  
 }  
  
 facebook.request( "me/feed", "POST", attachment ) -- posting the photo  
 end  
  
 -- This code posts a message to your Facebook Wall  
 if fbCommand == POST\_MSG then  
 local time = os.date("\*t")  
 local postMsg = {  
 message = "Posting from Corona SDK! " ..  
 os.date("%A, %B %e") .. ", " .. time.hour .. ":"  
 .. time.min .. "." .. time.sec  
 }  
  
 facebook.request( "me/feed", "POST", postMsg ) -- posting the message  
 end  
-----------------------------------------------------------------------------------------  
  
 elseif ( "request" == event.type ) then  
 -- event.response is a JSON object from the FB server  
 local response = event.response  
  
 if ( not event.isError ) then  
 response = json.decode( event.response )  
  
 if fbCommand == GET\_USER\_INFO then  
 statusMessage.textObject.text = response.name  
 printTable( response, "User Info", 3 )  
 print( "name", response.name )  
  
 elseif fbCommand == POST\_PHOTO then  
 printTable( response, "photo", 3 )  
 statusMessage.textObject.text = "Photo Posted"  
  
 elseif fbCommand == POST\_MSG then  
 printTable( response, "message", 3 )  
 statusMessage.textObject.text = "Message Posted"  
  
 else  
 -- Unknown command response  
 print( "Unknown command response" )  
 statusMessage.textObject.text = "Unknown ?"  
 end  
--[[  
 -- Display table of friends (not used at this time) \*\* Currently not used \*\*  
 local friends = {}  
 local data = response.data  
 for i=1,#data do  
 local name = data[i].name  
 table.insert( friends, name )  
 end  
  
 local topBoundary = display.screenOriginY + 40  
 local bottomBoundary = display.screenOriginY + 0  
  
 -- create the list of items  
 myList = tableView.newList{  
 data=friends,   
 default="listItemBg.png",  
 --default="listItemBg\_white.png",  
 over="listItemBg\_over.png",  
-- onRelease=listButtonRelease,  
 top=topBoundary,  
 bottom=bottomBoundary,  
 }  
--]]  
 else  
 -- Post Failed  
 statusMessage.textObject.text = "Post failed"  
 printTable( event.response, "Post Failed Response", 3 )  
 end  
  
 elseif ( "dialog" == event.type ) then  
 -- showDialog response  
 --  
 print( "dialog response:", event.response )  
 statusMessage.textObject.text = event.response  
 end  
end  
  
---------------------------------------------------------------------------------------------------  
-- NOTE: To create a mobile app that interacts with Facebook Connect, first log into Facebook  
-- and create a new Facebook application. That will give you the "API key" and "application secret"   
-- that should be used in the following lines:  
  
local appId = "xxxxxxxxxxxxxxxxxxx" -- Add your App ID here  
local apiKey = nil -- Not needed at this time  
---------------------------------------------------------------------------------------------------  
-- NOTE: You must provide a valid application id provided from Facebook  
  
if ( appId ) then  
  
 -- \*\*\*  
 -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Buttons Functions \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 -- \*\*\*  
 local function postPhoto\_onRelease( event )  
 -- call the login method of the FB session object, passing in a handler  
 -- to be called upon successful login.  
 fbCommand = POST\_PHOTO  
 facebook.login( appId, listener, {"publish\_stream"} )  
 end  
  
 local function getInfo\_onRelease( event )  
 -- call the login method of the FB session object, passing in a handler  
 -- to be called upon successful login.  
 fbCommand = GET\_USER\_INFO  
 facebook.login( appId, listener, {"publish\_stream"} )  
 end  
  
 local function postMsg\_onRelease( event )  
 -- call the login method of the FB session object, passing in a handler  
 -- to be called upon successful login.  
 fbCommand = POST\_MSG  
 facebook.login( appId, listener, {"publish\_stream"} )  
 end  
  
 local function showDialog\_onRelease( event )  
 -- call the login method of the FB session object, passing in a handler  
 -- to be called upon successful login.  
 fbCommand = SHOW\_DIALOG  
 facebook.login( appId, listener, {"publish\_stream"} )  
 end  
  
 local function tag\_onRelease( event )  
 -- call the login method of the FB session object, passing in a handler  
 -- to be called upon successful login.  
 fbCommand = SHOW\_DIALOG  
 facebook.login( appId, listener, {"publish\_stream"} )  
 end  
  
 local function logOut\_onRelease( event )  
 -- call the login method of the FB session object, passing in a handler  
 -- to be called upon successful login.  
 fbCommand = LOGOUT  
 facebook.logout()  
 end  
  
 -- \*\*\*  
 -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Create Buttons \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 -- \*\*\*  
  
 -- "Post Photo with Facebook" button  
 local fbButton = ui.newButton{  
 default = "fbButton184.png",  
 over = "fbButtonOver184.png",  
 onRelease = postPhoto\_onRelease,  
 text = " Post Photo",  
 x = 160,  
 y = ButtonOrigY  
 }  
  
 -- "Post Message with Facebook" button  
 local fbButton = ui.newButton{  
 default = "fbButton184.png",  
 over = "fbButtonOver184.png",  
 onRelease = postMsg\_onRelease,  
 text = "Post Msg",  
 x = 160,  
 y = ButtonOrigY + ButtonYOffset \* 1  
 }  
  
  
 -- "Get User Info with Facebook" button  
 local fbButton = ui.newButton{  
 default = "fbButton184.png",  
 over = "fbButtonOver184.png",  
 onRelease = getInfo\_onRelease,  
 text = "Get User",  
 x = 160,  
 y = ButtonOrigY + ButtonYOffset \* 3  
 }  
  
 -- "Logout with Facebook" button  
 local fbButton = ui.newButton{  
 default = "fbButton184.png",  
 over = "fbButtonOver184.png",  
 onRelease = logOut\_onRelease,  
 text = "Logout",  
 x = 160,  
 y = ButtonOrigY + ButtonYOffset \* 4  
 }  
else  
 -- Handle the response from showAlert dialog boxbox  
 --  
 local function onComplete( event )  
 if event.index == 1 then  
 system.openURL( "http://developers.facebook.com/docs/guides/canvas/" )  
 end  
 end  
  
 native.showAlert( "Error", "To develop for Facebook Connect, you need to get an API key and application secret. This is available from Facebook's website.",  
 { "Learn More", "Cancel" }, onComplete )  
end  
  
-- "Save Picture" button  
 local fbButton2 = ui.newButton{  
 default = "fbButton184.png",  
 over = "fbButtonOver184.png",  
 onRelease = saveImage,  
 text = "Take Pic",  
 x = 160,  
 y = ButtonOrigY + ButtonYOffset \* 2  
 }  
  
local sessionComplete = function(event)  
 local t = event.target  
  
 print( "Camera ", ( not t and "returned an image" ) or "session was cancelled" )  
 print( "event name: " .. event.name )  
 print( "target: " .. tostring( t ) )  
  
 local baseDir = system.DocumentsDirectory  
 display.save( t, "test.jpg", baseDir )  
  
 --testImage = display.newImage("test.jpg",system.DocumentsDirectory);  
  
end  
  
local saveImage = function( event )  
 media.show( media.Camera, sessionComplete )  
 return true  
end  
  
fbButton2:addEventListener('tap', saveImage)   

I apologize for any messy code here as I just trying to test getting the image from the Documents directory posted…

My app id has been replaced by x’s above.

Testing this on iPhone 4.

Thanks,
Tony [import]uid: 107466 topic_id: 22486 reply_id: 89909[/import]

Hrm, interesting. I have not yet had the chance to play with image posting a whole lot and am unaware of any limitations with the image location.

I’m going to ask some other members of the team and see if I can get some insight on this. [import]uid: 52491 topic_id: 22486 reply_id: 90080[/import]

You are a rock star! Thanks :slight_smile: [import]uid: 107466 topic_id: 22486 reply_id: 90192[/import]

Normally I would think there is a problem with the FB settings (on their site) and walk you through the steps to ensure you’re doing things properly, but since you said things work fine when you change the directory from system.DocumentsDirectory to system.ResourceDirectory and everything works fine, that sounds like there may be a bug within our internal FB implementation (thankfully, it doesn’t sound like a huge problem).

Would you mind wrapping up a sample, detailing the steps you took, and also a link to this thread and send in a bug report?

http://developer.anscamobile.com/content/bug-submission

Thanks. [import]uid: 52430 topic_id: 22486 reply_id: 90432[/import]

@John and Peach

Ok scratch the above issues because I got it to upload the photo I have saved from the camera.

I can post a photo to “me/photos” but posting a photo to “me/feed” does not post the photo…

I will look thru FB docs more but any quick tips on doing such a thing… posting an image to the feed/wall. [import]uid: 107466 topic_id: 22486 reply_id: 90644[/import]

Anyone familiar with posting an image to a friends wall as compared to updating your own wall from your app?

Trying to post/share the image I take with the camera onto a friends wall.

Adding their profile id as so “xxxxxx/photos” or “xxxxxx/feed” does not work in fact the image just posts to my own wall no matter what. [import]uid: 107466 topic_id: 22486 reply_id: 91301[/import]

Can anyone help me , I want to post photo on facebook but NOT with a URL ,
just by passing the saved image i tried both in Documents directory and in rescource directory both I am getting a white blank screen.

I referred a blog http://blog.anscamobile.com/2011/12/uploading-photos-to-facebook-in-corona/

but still am getting a blank white screen after I log in .

Please help asap

Thank u [import]uid: 95790 topic_id: 22486 reply_id: 94765[/import]

Seth,
Check the build you are using… I was able to successfully post from Documents and Resource.

The image posting requires you are using 2011.704 or higher.

Tony [import]uid: 107466 topic_id: 22486 reply_id: 96340[/import]

Hey Tony thanx ,

I am using build 767 i suppose it works fine

I am using JPG image.

ITs done I change Documents to Resources :slight_smile: thanx a ton :slight_smile:

[import]uid: 95790 topic_id: 22486 reply_id: 96490[/import]

hI ,anthony1 & seth.vedangi

But when use display.captureScreen( true ) the image will save in Corona Simulator folder in my PC with extension PNG ,how to use this image to share it on Facebook [import]uid: 160777 topic_id: 22486 reply_id: 118233[/import]