用于Facebook照片的Android多部分/表单数据编码
用于将照片上载到Facebook相册的新Facebook Android SDK的工作原理如下: 让我困惑的是用于Facebook照片的Android多部分/表单数据编码,android,facebook,Android,Facebook,用于将照片上载到Facebook相册的新Facebook Android SDK的工作原理如下: 让我困惑的是{image data},它说照片应该编码为多部分/表单数据,但是从params.putString(“source”,“{image data}”)我们可以看到putString()的第二个参数应该是字符串,如何对图像文件多部分/表单数据进行编码,并以字符串格式获取返回值? 像这样: public String getImageFormData(File image){ Stri
{image data}
,它说照片应该编码为多部分/表单数据
,但是从params.putString(“source”,“{image data}”)
我们可以看到putString()
的第二个参数应该是字符串
,如何对图像文件多部分/表单数据进行编码,并以字符串
格式获取返回值?
像这样:
public String getImageFormData(File image){
String imageValue;
...
return imageValue;
}
还是我理解错了,我现在的问题是我有图像文件,我如何使用上面的代码成功地将图像上传到Facebook?文档中的示例代码似乎是错误的。看起来您只需要一个名为“source”的(多部分)参数,并对图像数据进行编码
以下是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>");
}
您还可以尝试提供一个ParcelFileDescriptor
,它以类似的方式序列化:
public ParcelFileDescriptor getImageFormData(File image) {
try {
return ParcelFileDescriptor.open(image, ParcelFileDescriptor.MODE_READ_ONLY);
} catch (FileNotFoundException e) {
return null;
}
}
此方法可能也很有趣(允许您使用url参数而不是源):
将方法从putString更改为putByteArray谢谢您的帮助,我有一个问题,将bundle值转换为请求参数的代码没有返回任何结果,那么我应该上传什么到Facebook。你说文档中的代码似乎是错误的,请你也添加将照片发布到Facebook的代码好吗?@ZhenxiaoHao该代码来自Facebook SDK-只是为了了解他们如何处理捆绑包中的参数。而不是'params.putString(“source”,“{image data}”);',使用'params.putParcelable(“源”,位图);',其中“位图”是要上载的图像(以及位图类的实例)。
public Bitmap getImageFormData(File image) {
return BitmapFactory.decodeFile(image.getPath());
}
public ParcelFileDescriptor getImageFormData(File image) {
try {
return ParcelFileDescriptor.open(image, ParcelFileDescriptor.MODE_READ_ONLY);
} catch (FileNotFoundException e) {
return null;
}
}
/**
* 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);
}