Tech Tuesday: Posting to TwitPic from Adobe AIR

TwitPic is a service that lets you share photos on Twitter by generating a tiny url to the image being uploaded. The flash.filesystem.File class allows Adobe AIR to post an image to TwitPic. By specifying the username, password and posting method it is possible to submit a query to the TwitPic API.

[as]
package com.cb.twitpic
{
import flash.events.DataEvent;
import flash.events.Event;
import flash.filesystem.File;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

public class TwitPic
{
private var _file:File;

public function TwitPic()
{
_file = new File();
_file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
browse();
}
private function browse():void {
_file.addEventListener(Event.SELECT, fileSelected);
_file.browse( new Array( new FileFilter( “Images (*.jpg, *.jpeg, *.gif, *.png)”, “*.jpg;*.jpeg;*.gif;*.png” ) ) );
}

private function fileSelected(event:Event):void {
var urlRequest:URLRequest = new URLRequest(“http://twitpic.com/api/upload”);

// The API requires the request be sent via POST
urlRequest.method = URLRequestMethod.POST;

// Enter a valid Twitter username / password combination
var urlVars:URLVariables = new URLVariables();
urlVars.username = TWITTER_USERNAME;
urlVars.password = TWITTER_PASSWORD;
urlRequest.data = urlVars;

// The API requires the file be labeled as ‘media’
_file.upload(urlRequest, ‘media’);
}

private function uploadCompleteDataHandler(event:DataEvent):void
{
var resultXML:XML = new XML(event.text);

// Trace the path to the resulting image tiny url (mediaurl)
trace(resultXML.child(“mediaurl”)[0]);
}
}
}
[/as]
This can be used within an AIR application by importing TwitPic and initializing the class. An example can be supplied upon request.

This was originally posted on blackcj.com, Chris Black’s personal blog. Chris is a Senior Developer at Sierra Bravo.

Filed under Technology

Leave a Reply