Question

I am trying to upload a video to Facebook from an air for android app. the as3 facebook api has the following method for uploading videos
facebookMobile.uploadVideo(method:String, callback:Function = null, params:* = null)
params:* (default = null) — An object containing the title, description, fileName (including extension), and video (FileReference or ByteArray)

i am recording the video using cameraUi and am getting back the location of the file. according to the api i need to pass in this file using either fileReferance or a byteArray as i already have the location of the file i don't want any sort of browse functionality. I am having trouble creating the byteArray (never used it before). i am getting a 353 error from facebook: you must select a video file.

Bellow is my attempt at creating the bytearray

    public function UICompleteHandler(event:MediaEvent):void
    {
        trace("Welcome back from the camera");

        var media:MediaPromise = event.data;
        trace("file info "+media.file.url  + " - " + media.relativePath + " - " + media.mediaType);
        filePath = media.file.url;

        trace("Object encoding is: " + inBytes.objectEncoding + "\n\n" + "order file: \n\n");
        readFileIntoByteArray(filePath, inBytes); 
        trace("length 1:   "+inBytes.length);
        trace("position 1: "+inBytes.position);
        inBytes.position = 0; // reset position to beginning 
        //inBytes.uncompress(CompressionAlgorithm.DEFLATE); 

        //trace("position 2:  "+inBytes.position);
        //inBytes.position = 0;    //reset position to beginning 
        trace (inBytes);

    }
    private function readFileIntoByteArray(fileName:String, data:ByteArray):void 
    { 
        var inFile:File = new File(fileName);
        trace ("file to byte array  "+  inFile.url);
        trace ("file name var : "+fileName);

        inStream.open(inFile , FileMode.READ);
        inStream.readBytes(data); 
        inStream.close();           
    } 

and video upload code:

    public function handleUpload(ev:TouchEvent)
    {
        trace ("posting to facebook - FileName: "+  accessCamera.fileName + " - FilePath: " + accessCamera.filePath);
        var params:Object ={  
            title:'test upload on FB api',
            description:'test upload on FB api',
            fileName: accessCamera.fileName,
            video: accessCamera.inBytes
        }

        //trace ("params.video = "+params.video);

        FacebookMobile.uploadVideo('me/videos', onComplete, params);
    }

    private function onComplete( result:Object, fail:Object ):void {
        trace("facebook post onComplete called" );
        if (result)
        {
            //result.id is id of post that was just posted
            trace ("great");
        }
        else if (fail)
        {
            trace("post Failed");  
            trace('code: '+fail.error.code); 
            trace('message: '+fail.error.message);
            trace('type: '+fail.error.type); 
        }
    }
Was it helpful?

Solution

No need to convert it to a ByteArray. File is an AIR-only class meant to allow you to access the file system directly (as you already are doing). As File extends FileReference, you can simply pass the File object you already have instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top