4

I can successfully post a message in VKontakte (vk.com), but I can't figure out the way to post a message with an image in the attachment.

Method:

createWallPost(long owner_id, String text, Collection<String> attachments, String export, boolean only_friends, boolean from_group, boolean signed, String lat, String lon, String captcha_key, String captcha_sid);

So, if I use this method like this:

api.createWallPost(account.user_id, message, null, null, false, false, false, null, null, null, null);

It will post a message with some text successfully;

I have to use

Collection<String> attachments

parameter and somehow put a bitmap into collection. I can post a link of the picture, but I do not want a link, I want an embedded image. Any suggestions?

SDK is here (Russian):

Method

ozahorulia
  • 9,798
  • 8
  • 48
  • 72
ODAXY
  • 335
  • 4
  • 12
  • link asks for a login, but from what i gathered, there is a getPhotoUploadServer method to get the url to which upload a photo – njzk2 Jan 07 '13 at 18:06

3 Answers3

2

Upd. I update it this answear because VK released official sdk https://vk.com/dev/android_sdk. You should use it now for any purposes, it has documentation for all your needs too.

Ok, here's solution that perfectly works, I tried it myself:

Download Vkontakte SDK for Android: https://github.com/thest1/Android-VKontakte-SDK.

1) Upload an image to the server with function form here: https://github.com/devindi/Android-VKontakte-SDK/commit/342acbe76e97181fd1f09820504e47249c962640

/**
 * Upload image to user wall. Method requires httpmime library to create POST with image
 * you can download it here:http://hc.apache.org/downloads.cgi
 * direct link: http://mirrors.besplatnyeprogrammy.ru/apache//httpcomponents/httpclient/binary/httpcomponents-client-4.2.3-bin.zip
 * @param filePath absolutely path to image
 * @param userID
 * @return uploaded photo data (photo id and owner id)
 */
public Photo uploadPhotoToWall(String filePath, long userID){
    try {
        String uploadServer=photosGetWallUploadServer(userID, null);
        HttpClient client=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(uploadServer);
        MultipartEntity albumArtEntity = new MultipartEntity();
        albumArtEntity.addPart("photo", new FileBody(new File(filePath)));
        httpPost.setEntity(albumArtEntity);
        HttpResponse response=client.execute(httpPost);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        JSONObject photoObject = new JSONObject(builder.toString());
        return saveWallPhoto(photoObject.get("server").toString(), photoObject.get("photo").toString(), photoObject.get("hash").toString(), userID, null).get(0);
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (JSONException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings  File Templates.
    } catch (KException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

2) This function will return photo id, so you'll be able to use wall. savePost() method to post the bitmap. Like this:

long photoPid = account.api.uploadPhotoToWall(bitmapFilePath, account.user_id).pid;

                Collection<String> attachments = new ArrayList<String>();
                String att = "photo" + account.user_id + "_" + photoPid;
                attachments.add(att);
                api.createWallPost(account.user_id, text, attachments, null, false, false, false, null, null, null, null);

If you have any questions, feel free to ask.

Defuera
  • 5,356
  • 3
  • 32
  • 38
2

So, one more variant:

1) Download official VK android SDK https://github.com/VKCOM/vk-android-sdk

2) Authorize

3) See an example:

final Bitmap photo = getPhoto();
VKRequest request = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.9f)), 0, 0);
request.executeWithListener(new VKRequestListener() {
    @Override
    public void onComplete(VKResponse response) {
        photo.recycle();
        VKApiPhoto photoModel = ((VKPhotoArray) response.parsedModel).get(0);
        //Make post with photo
    }
    @Override
    public void onError(VKError error) {
        showError(error);
    }
});
Roman Truba
  • 4,401
  • 3
  • 35
  • 60
1
shareDialog.setAttachmentImages(new VKUploadImage[]{
new VKUploadImage(image, VKImageParameters.pngImage())
            });

and don't forget to: check image for null check VK permission for photoes (it's different from posting to wall permission)