twittpic

Put your problem here if it does not fit any of the other categories.

twittpic

Postby rayman » Fri Jan 22, 2010 5:43 pm

Hi,
Ive been trying to use this twittpic code in order to post image in twitter, but the new sdk doesnt support many of the classes there, like:Part,MultipartRequestEntity,ByteArrayPartSource,StringPart
mybe anyone could help me to convert this code to a code which i could use in our last sdk(2.1)

/** @see <a href="http://twitpic.com/api.do">Twitpic api</a> */
public void twitpic(String username, String password, byte[] iconBytes) throws IOException {

HttpClient client = Utils.getHttpClient();

PostMethod method = new PostMethod("http://twitpic.com/api/uploadAndPost");

ArrayList<Part> partList = new ArrayList<Part>();
{
partList.add(new StringPart("username", username));
partList.add(new StringPart("password", password));
partList.add(new StringPart("message", "My status icon - "));
partList.add(new FilePart("media",
new ByteArrayPartSource("image.png", _convertToPNG(iconBytes)), "image/png", null
));
}

MultipartRequestEntity entity = new MultipartRequestEntity(
partList.toArray(new Part[0]), method.getParams()
);

method.setRequestEntity(entity);
client.executeMethod(method);

}

/** given any byte[] containing an image (gif/jpg/png/etc), will convert it to a png image */
private byte[] _convertToPNG(byte[] imgBytes) throws IOException {
BufferedImage bufimg = ImageIO.read(new ByteArrayInputStream(imgBytes));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufimg, "png", baos);
baos.flush();
return baos.toByteArray();
}


Thanks,
ray.
rayman
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jan 11, 2010 11:26 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 14 guests