How to save a jpg image to android phone with as3 - android

I'm trying to save an image to my android phone is a specific directory.
Somehow I spent hours but couldn't get it to work. I hope someone can help me understand why my code isn't working or if there is another way to do it.
public function SaveTheImage(me:MouseEvent):void
{
ImageSaverBMD.draw(ImageHolder);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var jpgBytes:ByteArray = jpgEncoder.encode(ImageSaverBMD);
var myFile:File = File.documentsDirectory.resolvePath("/sdcard / DCIM / Camera/testingimage.jpg");
var fs:FileStream = new FileStream();
fs.open(myFile, FileMode.WRITE);
fs.writeBytes(jpgBytes, 0, jpgBytes.length);
fs.close();
}

There are many steps in your algorithm that could cause the bug, with the code you provided I could make a few guesses, but there is a better way.
First of all try this code:
var s:Shape = new Shape();
s.graphics.beginFill(0);
s.graphics.drawCircle( 20, 20, 20);
s.graphics.endFill();
var bd:BitmapData = new BitmapData(40, 40, false);
bd.draw(s);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var bytes:ByteArray = jpgEncoder.encode(bd);
var f:File = File.applicationDirectory;
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeBytes(bytes);
fs.close();
This should save a 80x80 pixels black circle in your provided directory. See if it works. If not: check permissions to have external storage.
If it works, start removing the code block-by-block from top to bottom:
Remove the top two blocks and put your ImageSaverBMD.draw(ImageHolder) line. Maybe the problem is with that class? It could be that the bitmap data size was wrong or the matrix for drawing had incorrect translation.
The JPGEncoder is probably OK, it's a common used framework, but sometimes when I copy it into my source files I need to change the package in the .as file. Is your package correct?
Your URL in the File object has spaces, could that be your problem, try using a simple URL first and see if that works. It could be the problem that you try to navigate to an absolute URL with a relative URL pattern (I could be wrong on this, usually I have different approach of navigating to a folder).
Hope that helps!

private function export():void
{
var today_date:Date = new Date();
var thismonth:uint = today_date.getMonth();
var today_time;
var currentTime:Date = new Date();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var hours = currentTime.getHours() * 30 + currentTime.getMinutes() / 2;
var mnth:Array = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var fileName:String = (today_date.getDate()+mnth[thismonth]+today_date.getFullYear()+"_"+currentTime.hours + currentTime.minutes + currentTime.seconds+".png");
//trace(fileName);// displays current date in United States date format
var bmd:BitmapData = new BitmapData(board.width, board.height);//(600, 290);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd);
// var file:File = File.applicationDirectory;
//var file:FileReference = new FileReference();
var fs : FileStream = new FileStream();
var targetFile : File = File.documentsDirectory.resolvePath(fileName);
//var targetFile : File = File.applicationDirectory.resolvePath(fileName);
fs.open(targetFile, FileMode.WRITE);
fs.writeBytes(ba);
fs.close();
//file.addEventListener(Event.COMPLETE, saveSuccessful);
saveDialog = new SaveDialog();
addChild(saveDialog);
test = setInterval(showMessage,2000);
//var test = setInterval(showMessage,3000);
saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeSaveDialog);
//file.save(ba, fileName);
}

Related

How to save multiple movie clip into an image air for android

I am creating a dress-up game, where the dynamic movie clip can also be saved as an image. Unfortunately, I was only able to save the image of one movie clip. Can anyone please help. I am new to flash.
Here is my code:
function fl_ClickToGoToAndStopAtFrame_18(event:MouseEvent):void
{
GirlDone_btn.removeEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_18);
GirlDone_btn.visible = false;
var jpgSource:BitmapData = new BitmapData(480, 386.15, false);
jpgSource.draw(GirlBg_mc);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var imgByteData:ByteArray = jpgEncoder.encode(jpgSource);
var file = new FileReference();
file.save(imgByteData, ".jpg");
if( CameraRoll.supportsAddBitmapData ) {
new CameraRoll().addBitmapData(jpgSource);
GirlDone_btn.visible = true;
GirlDone_btn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_18);
}
}

Xfinium PDF: Unable to save pdf file. Receiving error root missing entry

I have a problem saving the Xfinium PDF. I loaded a document and simply draw the line across the page and save. The file generated but when I used that file and loaded back to PdfFixedDocument I have the error
Root entry is missing in file trailer
My code is very simple :
var pdf = new PdfFixedDocument (document.Location);
var page = pdf.Pages [pageIndex];
var graphics = page.Graphics;
var directory = FileUtilities.GetExternalPrivateDirectory (PdfCore.CACHE_DIRECTORY);
//var png = FileUtilities.GetFile (directory + "/test.pdf");
//var rawStream = File.OpenWrite (png.AbsolutePath);
var stream = new FileStream (directory + "/test.pdf", FileMode.Create);
pdf.BeginSave (stream);
graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen (),
new Xfinium.Pdf.Graphics.PdfPoint (0,0),
new Xfinium.Pdf.Graphics.PdfPoint (page.Width, page.Height));
page.SaveGraphics ();
pdf.EndSave ();
Instead of manually creating the stream and using BeginSave / EndSave, just try using Save:
var pdf = new PdfFixedDocument();
var page = pdf.Pages.Add();
var graphics = page.Graphics;
graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen(),
new Xfinium.Pdf.Graphics.PdfPoint(0, 0),
new Xfinium.Pdf.Graphics.PdfPoint(page.Width, page.Height));
var directory = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
pdf.Save(Path.Combine(directory.Path, "test.pdf"));
Java.IO.File pdfFILE = new Java.IO.File(Path.Combine(directory.Path, "test.pdf"));
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Uri.FromFile(pdfFILE), "application/pdf");
StartActivity(intent);
I believe that the PDF document is not properly written to file because the stream is not closed. Try adding these lines at the end of your code (after pdf.EndSave()):
stream.Flush();
stream.Close();
The BeginSave/SaveGraphics/EndSave mode should be used when creating pages with heavy content, such as vector maps, and you want to reduce memory consumption by calling SaveGraphics after drawing a certain number of graphic objects (SaveGraphics can be called multiple times for a page).
For your scenario you can simplify the code as follows (like SushiHangover suggested):
var pdf = new PdfFixedDocument (document.Location);
var page = pdf.Pages [pageIndex];
var graphics = page.Graphics;
graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen (),
new Xfinium.Pdf.Graphics.PdfPoint (0,0),
new Xfinium.Pdf.Graphics.PdfPoint (page.Width, page.Height));
var directory = FileUtilities.GetExternalPrivateDirectory(PdfCore.CACHE_DIRECTORY);
pdf.Save(directory + "/test.pdf");
Disclaimer: I work for the company that develops XFINIUM.PDF library.
.Save (document path) is massively slow when it comes to e.g.: 100 pages and lots of data so the end file has about 145MB or more, then the saving operation takes 2 minutes pls - this is ways to slow so I will try the BeginSave and EndSave approach also

How do I code in actionscript the exact file location for several swf I have included for my apk?

In Air for Android Setting's 'Include files' part, I added test1.swf and test2.swf to the main file automatically included. Would the following be correct then:
myLoader.load(new URLRequest("test1.swf")
Or, is there any other folder I should include? Would the following be correct?
myLoader.load(new URLRequest("\..\..test1.swf")
I'm not in a position to test this right now on Android, but I believe included files are put in the application directory, which you can access as follows:
var path:String = File.applicationDirectory.resolvePath("test1.swf").nativePath;
myLoader.load(new URLRequest(path));
You can also use the shorthand url of:
myLoader.load(new URLRequest("app://test1.swf"));
You could also load the swf with the FileStream class, which gives you more control over things. That would look like this:
var file:File = File.applicationDirectory.resolvePath("test1.swf");
if (file.exists) {
var swfData:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(swfData);
stream.close();
var myLoader:Loader = new Loader();
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
myLoader.loadBytes(swfData, loaderContext);
addChild(myLoader);
}else {
trace("file doesn't exist");
}

as3 show loader while saving image to gallery

Am making an android app using AIR for Android and as3. Following is my code to save image into device gallery. The image gets saved but I want to show a loader while the image is getting saved. This code does not work approprietely. Please point out if I am making any mistake here.
mysaveBtn.addEventListener(MouseEvent.CLICK, ftnSave);
function ftnSave():void
{
mysaveBtn.removeEventListener(MouseEvent.CLICK, ftnSave);
mysaveBtn.visible = false;
var jpgSource:BitmapData = new BitmapData(1080, 1920);
jpgSource.draw(myTheme);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var imgByteData:ByteArray = jpgEncoder.encode(jpgSource);
var fs : FileStream = new FileStream();
var targetFile : File = File.desktopDirectory.resolvePath('/sdcard/DCIM/Camera/image.jpg');
fs.open(targetFile, FileMode.WRITE);
fs.writeBytes(imgByteData);
fs.close();
if( CameraRoll.supportsAddBitmapData ){
new CameraRoll().addBitmapData(jpgSource);
mysaveBtn.visible = true;
mysaveBtn.addEventListener(MouseEvent.CLICK, ftnSave);
}
}
You can use the class "Asynchronous Image Encoders"
This class can also save large images to prevent freezing your application .

as3 android saving remote sounds on sdcard - accessing later?

This code is way cool! Try it. It creates a folder in your user called .007 and saves an mp3 called yahoo.mp3.
It will also do it on an android device if permissions are set correctly.
Once it creates a folder and saves the mp3 it will loop thru the folder and get the file path and the file name.
MY QUESTION IS: How do you access it and play the sound on Android? It works on the desktop as you can see if you test it But not on the droid. Any ideas why?
import flash.filesystem.*;
var urlString:String = "http://YourWebsite.com/YourSound.mp3";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void
{
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
writeAirFile();
}
function writeAirFile():void
{
var file:File = File.userDirectory.resolvePath(".007/Yahoo.mp3");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
trace("The file is written.");
var desktop:File = File.userDirectory.resolvePath(".0");
var files:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++)
{
trace(files[i].nativePath);// gets the path of the files
trace(files[i].name);// gets the name
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
mySound.load(new URLRequest(files[0].nativePath));
myChannel = mySound.play();
}
}
file:// <-------The Answer!
Got it! The file path needed to access the "sdcard" on a Android phone must begin with: file://
If you want to load a sound from your phone it would look like this. Here is the one important line needed. I will post full code below.
mySound.load(new URLRequest("file://mnt/sdcard/AnyFolder/YourSound.mp3"));
Since I was using a loop above getting all the files in a particular folder this is the code that worked for me in the example above.
mySound.load(new URLRequest("file://"+files[0].nativePath));
FULL CODE TO LOAD AND PLAY SOUND ON DROID
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
mySound.load(new URLRequest("file://mnt/sdcard/AnyFolder/YourSound.mp3"));
myChannel = mySound.play();
I worked very hard on to find this answer. I hope this helps many. Please remember to +1 if this code has helped you. :)
I love StackOverflow! :)

Categories

Resources