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.

package com.cb.twitpic
	import flash.filesystem.File;
	public class TwitPic
		private var _file:File;
		public function TwitPic()
			_file = new File();
		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("");
			// 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; = 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)

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

Sample Code

17 thoughts on “Post to TwitPic from Adobe AIR

  1. It doesn’t look like Twitpic has a crossdomain.xml file. You may have to route the call through a PHP script located on your server. By passing the file through PHP you can avoid the cross domain issue. If I have some extra time this week I may be able to throw together a sample PHP file that would do this.

  2. hey Chris….
    please help me…
    i have a crossdomain.xml on my server but isn’t working…
    the content of mi xml file is the follow lines:
    <?xml version="1.0" encoding="utf-8"?>
    <site-control permitted-cross-domain-policies="all"/>
    <allow-access-from domain="*"/>
    <allow-access-from domain="*" secure="false"/>
    <allow-access-from domain="*" to-ports="*"/>
    <allow-http-request-headers-from domain="*" headers="*" secure="false"/>

    where is the error???
    please help me..

  3. Keep in mind that Adobe AIR generally does not require a crossdomain file since it uses localhost for all requests.

    A crossdomain file should site at the root of the domain in which you are accessing. You can check out flickrs crossdomain file for an example ( It is also possible to force Flash to use a specific policy file using Security.loadPolicyFile(“”). More information can be found at:

  4. hello.

    you shoud use relative URLs. put your file.php in the root folder of your site (that’s to say, in yourdomain/file.php) and use in flex this url: /file.php

    it worked for me.


  5. If the Flex and PHP are on the same domain you shouldn’t have any cross domain issues regardless of whether your path is relative or absolute. I personally prefer absolute URLs so the SWF can be moved around on the site without breaking the API calls. Relative URLs work for smaller sites that you are confident won’t be changed around much. Just to clarify, if you would like to use this example in Flex on the web you will want to communicate like this: Flex web app – PHP on your server – TwitPic API. You can not currently connect directly to the TwitPic API from a Flex web app because they do not have a policy file. If your running this through Adobe AIR (like in the example above) you can ignore this comment. Cheers.

  6. hi chris

    in a flash web application how do i make it work
    urlVars.username = TWITTER_USERNAME;
    urlVars.password = TWITTER_PASSWORD; = byteArray; // this is a binnaty array of an movieClip
    urlVars.message = “New tweet”;
    var urlRequest:URLRequest = new URLRequest(“”); = urlVars;
    urlRequest.method = URLRequestMethod.POST;

    // create the image loader & send the image to the server;
    var urlLoader : URLLoader = new URLLoader();
    //urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    urlLoader.load( urlRequest );

    but this doesn’t seem to do anything i can use filereference for web applications but how to i convert bytearray to filereference.

    Please suggest.

  7. Hi Chris,

    I´m developing a FlashPlayer app that use TwitPic API, but I need pass MovieClip as”media”. I tried to turn this MovieClip into ByteArray (through BitmapData and JPGEncoder) and pass “media” in URLVariables, but a got this response:

    Can you help me?

    Nice work with this post!!

  8. Filipe,

    Check out my comment to Shammi above. I’ve got another blog post that has source code that should be exactly what you’re looking for. Please feel free to contact me if that post doesn’t answer your question.



  9. This post is for Adobe AIR which uses localhost. On the web, Flash content requires a crossdomain.xml file for accessing files on another server. TwitPic only allows requests coming from their server. You will need a proxy on your local server in order to get around this. Please see my post on setting up a Twitter proxy:

    Setting up a TwitPic proxy would be similar. For more information on policy files please read this article:

Comments are closed.