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.

Actionscript:
  1. package com.cb.twitpic
  2. {
  3.     import flash.events.DataEvent;
  4.     import flash.events.Event;
  5.     import flash.filesystem.File;
  6.     import flash.net.FileFilter;
  7.     import flash.net.URLRequest;
  8.     import flash.net.URLRequestMethod;
  9.     import flash.net.URLVariables;
  10.    
  11.     public class TwitPic
  12.     {
  13.         private var _file:File;
  14.        
  15.         public function TwitPic()
  16.         {
  17.             _file = new File();
  18.             _file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
  19.             browse();
  20.         }
  21.         private function browse():void {
  22.             _file.addEventListener(Event.SELECT, fileSelected);
  23.             _file.browse( new Array( new FileFilter( "Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png" ) ) );
  24.         }
  25.        
  26.         private function fileSelected(event:Event):void {
  27.             var urlRequest:URLRequest = new URLRequest("http://twitpic.com/api/upload");
  28.            
  29.             // The API requires the request be sent via POST
  30.             urlRequest.method = URLRequestMethod.POST;
  31.            
  32.             // Enter a valid Twitter username / password combination
  33.             var urlVars:URLVariables = new URLVariables();
  34.             urlVars.username = TWITTER_USERNAME;
  35.             urlVars.password = TWITTER_PASSWORD;
  36.             urlRequest.data = urlVars;
  37.            
  38.             // The API requires the file be labeled as 'media'
  39.             _file.upload(urlRequest, 'media');
  40.         }
  41.        
  42.         private function uploadCompleteDataHandler(event:DataEvent):void
  43.         {
  44.             var resultXML:XML = new XML(event.text);
  45.            
  46.             // Trace the path to the resulting image tiny url (mediaurl)
  47.             trace(resultXML.child("mediaurl")[0]);
  48.         }
  49.     }
  50. }

This can be used within an AIR application by importing TwitPic and initializing the class.

Sample Code
http://www.blackcj.com/PostTwitPicFromAIR.zip

Tags: , , , , ,