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

4

3 回答 3

2

所以,还有一个变种:

1) 下载官方VK android SDK https://github.com/VKCOM/vk-android-sdk

2) 授权

3)看一个例子:

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);
    }
});
于 2014-03-24T08:31:42.510 回答
2

更新。我更新它这个答案是因为 VK 发布了官方 sdk https://vk.com/dev/android_sdk。您现在应该将它用于任何目的,它也有满足您所有需求的文档。

好的,这是完美的解决方案,我自己尝试过:

下载适用于 Android 的 Vkontakte SDK:https ://github.com/thest1/Android-VKontakte-SDK 。

1) 上传图片到服务器,这里有函数形式: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)此函数将返回照片ID,因此您将能够使用墙。savePost() 方法来发布位图。像这样:

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);

如果您有任何问题随时问。

于 2013-09-14T15:04:40.907 回答
1
shareDialog.setAttachmentImages(new VKUploadImage[]{
new VKUploadImage(image, VKImageParameters.pngImage())
            });

并且不要忘记:检查图像是否为空检查照片的VK权限(这与发布到墙上的权限不同)

于 2015-01-08T14:23:26.530 回答