使用Android应用,我正在尝试使用graph-api参考将照片发布到脸谱墙: 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();
我可以轻松地将图片传递图片网址上传到“源”,但我想从我的FileInputStream发送multipart / form-data而不上传到服务器。
有人可以解释我如何从“照片,编码为表单数据”生成字符串? 我试过这种方法,但似乎不起作用:
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();
}
非常感谢你。
答案 0 :(得分:2)
它以这种方式对图像进行编码:
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);