Upload image to vk.com

I can successfully post a message to VKontakte (vk.com), but I canโ€™t understand how to send a message with an image in the application.

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 as follows:

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

He will post a message with some text successfully;

I need to use

 Collection<String> attachments 

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

SDK here (Russian):

Method

+4
source share
3 answers

Upd. I am updating this post because VK has released the official sdk https://vk.com/dev/android_sdk . You must use it now for any purpose, it has documentation for all your needs.

Ok, here is a solution that works great, I tried it myself:

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

1) Upload the image to the server with the 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 the photo identifier, so you can use the wall. savePost () to place 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.

+2
source

So, another option:

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

2) Log in

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); } }); 
+2
source
 shareDialog.setAttachmentImages(new VKUploadImage[]{ new VKUploadImage(image, VKImageParameters.pngImage()) }); 

and donโ€™t forget: check the image for null check the VK resolution for photos (this differs from the permission to publish on the wall)

+1
source

All Articles