用于Facebook照片的Android多部分/表单数据编码



用于将照片上传到Facebook相册的新Facebook Android SDK的工作方式如下:

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

让我一头雾水的是{image-data},它说照片应该编码为multipart/form-data,但是从params.putString("source", "{image-data}")我们可以看出putString()的第二个参数应该是String,如何multipart/form-data编码图像文件并获取String格式的返回值? 喜欢这个:

public String getImageFormData(File image){
String imageValue;
...
return imageValue;
}

还是我理解错了,我现在的问题是我有图片文件,如何使用上面的代码成功将图片上传到Facebook?

文档中的示例代码似乎是错误的。似乎您所需要的只是一个名为"源"的(多部分)参数,其中包含对图像数据进行编码。

以下是来自Facebook Android SDK的代码,用于将捆绑包值转换为请求参数:

public void writeObject(String key, Object value) throws IOException {
if (isSupportedParameterType(value)) {
writeString(key, parameterToString(value));
} else if (value instanceof Bitmap) {
writeBitmap(key, (Bitmap) value);
} else if (value instanceof byte[]) {
writeBytes(key, (byte[]) value);
} else if (value instanceof ParcelFileDescriptor) {
writeFile(key, (ParcelFileDescriptor) value, null);
} else if (value instanceof ParcelFileDescriptorWithMimeType) {
writeFile(key, (ParcelFileDescriptorWithMimeType) value);
} else {
throw new IllegalArgumentException("value is not a supported type: String, Bitmap, byte[]");
}
}
public void writeBitmap(String key, Bitmap bitmap) throws IOException {
writeContentDisposition(key, key, "image/png");
// Note: quality parameter is ignored for PNG
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
writeLine("");
writeRecordBoundary();
logger.appendKeyValue("    " + key, "<Image>");
}

特别是,对于捆绑包中的任何位图,它们将序列化并为其创建适当的多部分标头。您可以尝试将图像作为位图添加到捆绑包中。然后,您的getImageFormData方法可以是这样的:

public Bitmap getImageFormData(File image) {
return BitmapFactory.decodeFile(image.getPath());
}

您也可以尝试提供一个ParcelFileDescriptor,它以类似的方式进行序列化:

public ParcelFileDescriptor getImageFormData(File image) {
try {
return ParcelFileDescriptor.open(image, ParcelFileDescriptor.MODE_READ_ONLY);
} catch (FileNotFoundException e) {
return null;
}
}

此方法也可能感兴趣(允许您使用 url 参数而不是源):

/**
* Creates a new Request configured to upload an image to create a staging resource. Staging resources
* allow you to post binary data such as images, in preparation for a post of an Open Graph object or action
* which references the image. The URI returned when uploading a staging resource may be passed as the image
* property for an Open Graph object or action.
*
* @param session
*            the Session to use, or null; if non-null, the session must be in an opened state
* @param image
*            the image to upload
* @param callback
*            a callback that will be called when the request is completed to handle success or error conditions
* @return a Request that is ready to execute
*/
public static Request newUploadStagingResourceWithImageRequest(Session session,
Bitmap image, Callback callback) {
Bundle parameters = new Bundle(1);
parameters.putParcelable(STAGING_PARAM, image);
return new Request(session, MY_STAGING_RESOURCES, parameters, HttpMethod.POST, callback);
}

将方法从putString更改为putByteArray

最新更新