gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:25 26 4
gpt4 key购买 nike

用于将照片上传到 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?

最佳答案

文档中的示例代码似乎有误。似乎您所需要的只是一个名为“source”的(多部分)参数,其中包含已编码的图像数据。

下面是来自 Facebook Android SDK 的代码,用于将 Bundle 值转换为请求参数:

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 中的任何位图,它们会为其序列化并创建适当的多部分 header 。您可以尝试将图像作为位图添加到 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);
}

关于Facebook 照片的 Android 多部分/表单数据编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21561331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com