gpt4 book ai didi

android - 如何使用 multipart/form-data 发布 facebook 照片

转载 作者:行者123 更新时间:2023-11-29 21:17:56 24 4
gpt4 key购买 nike

使用 Android 应用程序,我尝试使用 graph-api 引用将照片发布到 facebook 墙上: https://developers.facebook.com/docs/graph-api/reference/user/photos/

Bundle params = new Bundle();
params.putString("source", "{image-data}");
/* make the API call */
new Request(
session,
"/me/photos",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();

我可以轻松地将图片通过图像 url 上传到“源”,但我想从我的 FileInputStream 发送多部分/表单数据而不上传到服务器。

有人可以向我解释如何从“照片,编码为表单数据”生成字符串吗?我已经尝试过这种方法,但它似乎不起作用:

static final String GetMultipartFormData(InputStream fileInputStream)
{
StringBuilder sb = new StringBuilder();

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

try
{
sb.append(twoHyphens + boundary + lineEnd);
sb.append("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"fileName.jpg\"" + lineEnd);
sb.append(lineEnd);

InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}

sb.append(lineEnd);
sb.append(twoHyphens + boundary + twoHyphens + lineEnd);

fileInputStream.close();

}
catch (Exception ex)
{
ex.printStackTrace();
}

return sb.toString();
}

非常感谢。

最佳答案

它以这种方式对图像进行编码:

Bitmap photo; //This is the bitmap of your photo
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

并将图像传递给 Bundle:

params.putByteArray("source",byteArray);

关于android - 如何使用 multipart/form-data 发布 facebook 照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21054791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com