如何使用msgraph-sdk-java更新用户个人资料照片?

时间:2018-11-23 16:54:51

标签: microsoft-graph

这是我到目前为止所拥有的:

    ProfilePhoto photo = new ProfilePhoto();
    photo.???


    IProfilePhotoRequest request = graphServiceClient.users(userId).photo().buildRequest();
    request.patch(photo, new ICallback<ProfilePhoto>(){
        @Override
        public void success(final ProfilePhoto result) {

        }

        @Override
        public void failure(ClientException e) {

        }
    });

但是我不知道如何设置“图像的二进制数据”:

输入https://graph.microsoft.com/v1.0/me/photo/ $ value 内容类型:image / jpeg

图片的二进制数据

1 个答案:

答案 0 :(得分:0)

您需要使用BaseProfilePhotoStreamRequest,而不要使用代码中的那个。这样的事情:

IBaseProfilePhotoStreamRequest request = graphServiceClient.users(userId).photo().getContent().buildRequest();
request.put(imageBytes);

获取图像二进制文件的参考代码:

import java.awt.image.BufferedImage;   
import java.io.ByteArrayInputStream;   
import java.io.ByteArrayOutputStream;   
import java.io.File;   
import java.io.IOException;   

import javax.imageio.ImageIO;   

import sun.misc.BASE64Decoder;   
import sun.misc.BASE64Encoder;   

public class TestImageBinary {   
    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();   
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();   

    public static void main(String[] args) {   
        System.out.println(getImageBinary());   

        base64StringToImage(getImageBinary());   
    }   

    static String getImageBinary(){   
        File f = new File("c://20090709442.jpg");          
        BufferedImage bi;   
        try {   
            bi = ImageIO.read(f);   
            ByteArrayOutputStream baos = new ByteArrayOutputStream();   
            ImageIO.write(bi, "jpg", baos);   
            byte[] bytes = baos.toByteArray();   

            return encoder.encodeBuffer(bytes).trim();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return null;   
    }  
}