Android Multipart / form-data encoding for Facebook

The new Facebook Android SDK for uploading photos to a Facebook album works as follows 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();

What confuses me: {image-data}he said that the photo should be encoded as multipart/form-data, but from params.putString("source", "{image-data}")we can see that the second parameter putString()should be String, how can I encode the image file multipart/form-dataand get the return value in the format String? Like this:

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

    return imageValue;
}

Or I understand that this is wrong, now my question is that I have an image file, how can I use the above code to successfully upload the image to Facebook?

+4
source share
2

, -, . , , , (multipart) "" .

Android 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 . 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);
}
+6

putString putByteArray

0

Source: https://habr.com/ru/post/1525293/


All Articles