Using the Facebook SDK to upload a screenshot to Unity

I am trying to use the Facebook SDK to upload screenshots to Facebook using FB.Feed() instead of FB.API() so that users can post to their post. I was able to get this to work in the Unity Editor, but when I tried it on my Android, I get:

"A bad parameter was specified in this dialog box.

API Error Code: 100
API Error Description: Invalid parameter
Error message: Image url is not formatted properly "

And here is the code I wrote:

 var width = Screen.width; var height = Screen.height; var tex = new Texture2D(width, height, TextureFormat.RGB24, false); // Read screen contents into the texture tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex.Apply(); byte[] screenshot = tex.EncodeToPNG(); File.WriteAllBytes(Application.persistentDataPath + "/Screenshot.png", screenshot); //Application.CaptureScreenshot(Application.persistentDataPath + "/Screenshot.png"); Debug.Log(Application.persistentDataPath + "/Screenshot.png"); Debug.Log("Does File Exist? " + File.Exists(Application.persistentDataPath + "/Screenshot.png")); Debug.Log("File://" + Application.persistentDataPath + "/Screenshot.png"); FB.Feed( picture: "File://" + Application.persistentDataPath + "/Screenshot.png" ); 
+3
source share
1 answer

You cannot upload an image to facebook using FB.Feed() , picture parametr must be a URL on the Internet.

To upload an image, you can use the FB.API("me/photos") method

 private void TakeScreenshot() { var width = Screen.width; var height = Screen.height; var tex = new Texture2D(width, height, TextureFormat.RGB24, false); // Read screen contents into the texture tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex.Apply(); byte[] screenshot = tex.EncodeToPNG(); WWWForm wwwForm = new WWWForm(); wwwForm.AddBinaryData("image", screenshot, "screenshot.png"); FB.API("me/photos", HttpMethod.POST, CallBack, wwwForm); } 
0
source

Source: https://habr.com/ru/post/969971/


All Articles