用于将照片上传到Facebook相册的新Facebook Android SDK就像这样link:
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?
答案 0 :(得分:6)
文档中的示例代码似乎有误。您似乎只需要一个名为“source”的(多部分)参数,并对图像数据进行编码。
以下是用于将Bundle值转换为请求参数的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>");
}
特别是,对于Bundle中的任何位图,它们都会序列化并为其创建适当的multipart标头。您可以尝试将图像作为位图添加到Bundle中。您的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);
}
答案 1 :(得分:0)
将方法从 putString 更改为 putByteArray