将图像上传到vk.com

时间:2013-01-07 17:54:33

标签: android api vk

我可以在VKontakte(vk.com)中成功发布消息,但我无法找到在附件中发布带有图像的消息的方法。

方式:

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

所以,如果我像这样使用这个方法:

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

它会成功发布包含某些文字的消息;

我必须使用

Collection<String> attachments

参数并以某种方式将位图放入集合中。 我可以发布图片的链接,但我不想要链接,我想要一个嵌入的图像。有什么建议?

SDK is here (Russian):

Method

3 个答案:

答案 0 :(得分:2)

UPD。我更新了这个answear,因为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);

如果您有任何疑问,请随时提出。

答案 1 :(得分: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);
    }
});

答案 2 :(得分:1)

shareDialog.setAttachmentImages(new VKUploadImage[]{
new VKUploadImage(image, VKImageParameters.pngImage())
            });

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

相关问题