用于将照片上传到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();
Run Code Online (Sandbox Code Playgroud)
令我困惑的是{image-data},它说照片应该被编码为multipart/form-data,但从params.putString("source", "{image-data}")我们可以看到第二个参数putString()应该是a String,我如何编码图像文件multipart/form-data并获得String格式的返回值?像这样:
public String getImageFormData(File image){
String imageValue;
...
return imageValue;
}
Run Code Online (Sandbox Code Playgroud)
或者我理解错了,我现在的问题是我有图像文件,如何使用上面的代码将图像成功上传到Facebook?
文档中的示例代码似乎是错误的.您似乎只需要一个名为"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>");
}
Run Code Online (Sandbox Code Playgroud)
特别是,对于Bundle中的任何位图,它们为它序列化并创建适当的multipart标头.您可以尝试将图像作为位图添加到Bundle中.您的getImageFormData方法可能是这样的:
public Bitmap getImageFormData(File image) {
return BitmapFactory.decodeFile(image.getPath());
}
Run Code Online (Sandbox Code Playgroud)
您也可以尝试提供一个ParcelFileDescriptor以类似方式序列化的:
public ParcelFileDescriptor getImageFormData(File image) {
try {
return ParcelFileDescriptor.open(image, ParcelFileDescriptor.MODE_READ_ONLY);
} catch (FileNotFoundException e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
此方法也可能是有意义的(允许您使用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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1352 次 |
| 最近记录: |