How to share the image in the application as3 - actionscript-3

I am using the Android-Sharing-Extension-ANE.
How to share an image as result from my application code?
This code does not work
var bitmap:Bitmap = ...;
// encoding image by native encoder (availible on FP 11.3/AIR 3.3 or newer)
var bitmapBytes:ByteArray = bitmap.bitmapData.encode(new Rectangle(0, 0, bitmap.width, bitmap.height), new JPEGEncoderOptions(70)));
var file:File = File.documentsDirectory.resolvePath("image_for_share.jpg");
var stream:FileStream = new FileStream(); // write file to local memory
stream.open(file, FileMode.WRITE);
stream.writeBytes(fileBytes);
stream.close();
SharingExtension.shareImage(file, "Choser title", "Message"));

"How do I share an image with this ANE I use Animate CC?"
From the "Read Me" of that ANE:
How to use:
Connect com.illuzor.extensions.SharingExtension.ane file to your
Android AIR project.
Import com.illuzor.sharingextension.SharingExtension;
Since it's not clear what your exact problem is (because not enough information given)...
(1) Did you import the required Class files?
import com.illuzor.sharingextension.SharingExtension;
btn_Share.addEventListener (MouseEvent.CLICK, shareAndroid);
function shareAndroid (event: MouseEvent): void
{
//# If this works then replace with bitmap code (use shareImage)
SharingExtension.shareText ("AS3 Test", "This text is from AS3 code");
}
(2) Did you add (connect) the ANE to your project (in Settings)?
Read this for advise on adding an ANE: https://www.adobe.com/devnet/air/articles/using-ane-in-flash.html

Related

AS3 FLASH AIR - Loader not finding url

I'm having an issue with loading an image using the Loader class.
Can someone see what I'm doing wrong?
// get file folder location
var file = new File(File.applicationStorageDirectory.nativePath);
// convert to string
var fileString:String = file.url.toString();
// remove string characters
fileString = fileString.split('file:///').join('');
// create loader
var loader:Loader = new Loader();
// create request
var urlReq:URLRequest = new URLRequest(fileString+'/logo.jpg');
// load request
loader.load(urlReq);
When I test it gives me a 'Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.' If I use a loader.contentLoaderInfo to check IOERROR.IO_ERROR it gives me '1119 Access of possibly undefined propety IOERROR through a reference with static type flash.display:Loader'
Any thoughts to what I'm doing wrong? I've also tried just loading the .jpg from the same folder
var urlReq = new URLRequest('logo.jpg');
that the test app is in but still get the "URL Not Found"
Any help would be greatly appreciated.
Thank you.
This is simple: Don't use Loader, use FileStream instead. Since the file is saved into the app storage dir (or I assume it is, at least), you can read it directly rather than using a Loader.
var file:File = File.applicationStorageDirectory.resolvePath( "logo.jpg" );
var fs:FileStream = new FileStream();
fs.open( file, FileMode.READ );
var bmp:Bitmap = fs.readObject();
fs.close();
this.addChild( bmp );
You should avoid using Loader as much as possible. There is a lot of extra weight in the Loader class that adds can be a drain on performance. Use Bitmap instead, which is the lowest level way of display an image, and wrap it in a Sprite (or use Image instead of Bitmap and Sprite if using Flex) if you need to give it interactivity.

Image path issue on as3

I'm trying to create a screensaver for one our of our display we have at work. Images will be uploaded to an external server, from that server I will have pull the images and xml file. so my flash app and my content will be in two different places. I'm getting an error "SecurityError: Error #2000: No active security context". how do I override error and get the images to my stage.
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML;
var imageList:XMLList;
var imageLoader:Loader = new Loader();
var timer:Timer =new Timer(5000);
var imageIndex:uint = 0;
var child:DisplayObject;
var path:String="http://bgxserv03.mgmmirage.org/interactivemedia/mmhub01/test/mb/edit_bay/hr/infoscreen/servamb/";
xmlLoader.load(new URLRequest(path +"output.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
timer.addEventListener(TimerEvent.TIMER, tick);
function xmlLoaded(e:Event) {
xmlData = new XML ( e.target.data);
imageList = xmlData.image.name;
timer.start();
loadImage(imageList[0]);
}
function imageLoaded(e:Event){
if (child){
myImageHolder.removeChild(child);
}
child = myImageHolder.addChild(imageLoader);
Tweener.addTween(child, {alpha:0, time:1, delay:4});
Tweener.addTween(child, {alpha:1, time:1, delay:5});
}
function loadImage(path:String){
imageLoader.load(new URLRequest( path +"photos/"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Any help would be deeply appreciate. Thank you.
You need to put the "Crossdomain.XML" on the server's root directory. This will allow your flash file to access the data (image in your case) from that server. You can get a sample xml from the following URL, customize it for your server:
Sample CrossDomain.XML
What you are missing is probably a crossdomain.xml policy file at the domain of your image/xml files.
Use this link to create a crossdomain.xml file and add it to the root of your image/xml domain like so : "http://bgxserv03.mgmmirage.org/crossdomain.xml"
The URLLoader load() function automatically checks for the crossdomain.xml. Loader class requires you specify that you are interested in checking for a policy file in a LoaderContext object sent to the load() function.
In your code, it looks like the error should be coming from the URLLoader xml file request, since it doesn't look like you are trying to access the bitmap data of your images in any way, which is normally what would throw a security error for image files. If it is a problem with the image loading part, then complete the following instructions and you should be set to go:
In your loadImage function, add a LoaderContext parameter to your load method call:
function loadImage(path:String){
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.checkPolicyFile = true;
imageLoader.load(new URLRequest( path +"photos/"), loaderContext);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Check out the spec for more info on how to use the Loader class.
If you run into any trouble, this thread may be helpful.

loading images and video in AIR without HTTP

I'm curious what the correct methodology is for loading image and video data directly from the file system, without employing HTTP.
I'm writing an AIR slideshow application, and it works great but currently relies on a local MAMP server to hand the app all the media via the standard, tried and true, FLASH media loading methodologies.
I know that since FLASH was developed as a web plugin it handles this way of receiving data best, but I'd really like to extricate this rather onerous and unnecessary bit and have the app as a stand-alone player, however, I'm unclear what the "correct" way is to load the media.
I have the File objects ready and I've gotten as far as having the user select the local directory from which to pull the media and I'm getting a list of the files (based on their extensions) from the list... but now what?
You first have to put the content of your file in a ByteArray (code from FileStream documentation)
var bytes:ByteArray = new ByteArray();
var myFileStream:FileStream = new FileStream();
var myFile:File = File.documentsDirectory.resolvePath("test.jpg");
myFileStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
myFileStream.openAsync(myFile, FileMode.READ);
function progressHandler(event:ProgressEvent):void
{
if (myFileStream.bytesAvailable)
{
myFileStream.readBytes(bytes, myFileStream.position, myFileStream.bytesAvailable);
}
else
{
myFileStream.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
loadImage();
}
}
Then you may load these bytes in a Loader to display the image (see method Loader.loadBytes)
function loadImage():void
{
var loader:Loader = new Loader();
loader.loadBytes(bytes);
addChild(loader);
}
Cheers
With this code the Loader fires the Event.COMPLTE event and you will be able to manipulate the Bitmap from it:
var bytes:ByteArray = new ByteArray();
var myFileStream:FileStream = new FileStream();
var myFile:File = File.documentsDirectory.resolvePath(YOUR_PATH);
myFileStream.addEventListener(Event.COMPLETE, fileCompleteHandler)
myFileStream.openAsync(myFile, FileMode.READ);
function fileCompleteHandler(event:Event):void
{
myFileStream.readBytes(bytes);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader.loadBytes(bytes);
function imageLoaded(e:Event):void
{
addChild(Bitmap(loader.content));
myFileStream.removeEventListener(Event.COMPLETE, fileCompleteHandler);
myFileStream.close();
}
}
PS: Thanks Kodiak, I made my code based on yours.

How to load image in Action Script?

How to load image in Action Script ?
i have used the following code but image was not loaded.`var ldr:Loader = new Loader();
var url:String = "D:\BlackBerry\workspace\SoundTest\blackberry-tablet-icon.png.bmp";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
addChild(ldr);
stage.nativeWindow.visible = true;`
Please help ?
You need to check the errors that this loader gives:
ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
ldr.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
function onError(e:IOErrorEvent):void{
trace(e);
}
function onStatus(e:HTTPStatusEvent):void{
trace(e);
}
Now you can filter the errors out and search on the web the specific arrow this loader gives.
I guess its a security error. Flash prevents to load images outside his domain(exception is debug version in Flash cs5)
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/SecurityError.html

AS3 Saving Local File under iOS

I am opening and testing data using the FileStream class which is supposed to avoid file security issues. It works fine until I try to save to a local file. When I test under adl I get a security error which I though was ok (at least it was attempting to save) until the app was packaged and tested on a device, but the file fails to save. Any ideas?
public class FilesApp extends MovieClip {
var file:File;
var xmldata:XML;
public function FilesApp() {
// constructor code
LoadButton.addEventListener(MouseEvent.MOUSE_DOWN,xml_load);
ParseButton.addEventListener(MouseEvent.MOUSE_DOWN,xml_parse);
SaveButton.addEventListener(MouseEvent.MOUSE_DOWN,xml_save);
}
public function xml_load (e:MouseEvent):void
{
file = File.applicationDirectory;
file = file.resolvePath("./Data/data.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
fileStream.close();
DataText.text = str;
xmldata = XML(str);
trace('xml file loaded');
}
public function xml_save (e:MouseEvent):void
{
var writeStream:FileStream = new FileStream();
writeStream.open(file, FileMode.WRITE);
writeStream.writeUTF("<xml><name>Changed</name><score>5000</score></xml>");
writeStream.close();
trace('xml file saved');
}
public function xml_parse (e:MouseEvent):void
{
trace('xml file saved');
DataText.text = "Your name is:"+xmldata.name+" Your best score is:"+xmldata.score;
}
}
You cannot save data in File.applicationDirectory, it is meant to be immutable. Store it in File.applicationStorageDirectory instead. There is also File.documentsDirectory - current user documents directory, if you are sure you need to litter it with your files :)
For me, switching to readUTF() worked.
Also, the two lines initializing "file" should be in the constructor, else you cannot call "xml_save" (you'll have to do "xml_load" first).
Thanks
did you try Filereference.save()?
It fails because you are using writeUTF to do the write and readMultiByte to do the read.
If you switch to readUTF it will work.