简体   繁体   中英

Using Facebook SDK to Upload Screenshot in Unity

I'm am trying to use Facebook SDK to Upload Screenshots to Facebook using FB.Feed() instead of FB.API() so that users can write a message to their post. I was able to get this to work on Unity Editor but when I try it on my Android, I get:

"This dialog has been passed a bad parameter.

API Error Code: 100
API Error Description: Invalid parameter
Error Message: picture URL is not properly formatted"

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"
);

You can't upload image to facebook using FB.Feed() , picture parametr must be url in web.

To upload image you may use 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM