借助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();
我可以轻松地将通过图片的图片网址上传到“源”,但是我想从我的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);