Can't find image exported from AIR app on Android phone - actionscript-3

I feel as if I'm missing something very basic here. I'm trying to add an option to let export images from an Android app in PNG format. The code seems to work fine on my Droid Bionic and doesn't error out, but afterwards I can't find the image anywhere in the phone's storage. Where is it going? Is it possible to access images exported from an AIR app? Am I missing something completely obvious?
bdToSave.draw(screenshot);
var arrBytes:ByteArray = PNGEncoder.encode(bdToSave);
var file:File = File.desktopDirectory.resolvePath("testimage.png");
var fileStream:FileStream = new FileStream();
try {
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(arrBytes);
fileStream.close();
}
catch (errObject:Error) {
Env.outBox.text = "Error writing image file!"
}
I've also used the CameraRoll class to successfully export images and found the resulting files easily, but the images there seemed to use bad JPEG compression. So I was hoping using FileStream instead would result in nicer images.

File.desktopDirectory does not exist on Android. Use File.applicationDirectory or File.applicationStorageDirectory to write to the internal disk. You almost always want to use applicationStorageDirectory and I believe that is actually the only one you can use on iOS for writing data. For writing to external drives, and this is untested on my part, I believe you want to use File.documentsDirectory. Trace out File.documentsDirectory.nativePath to verify that is the correct path.
It might be wise to consider that the majority of higher-end phones being released now do not contain SD cards. If this is meant to be a mass market product, I'd recommend using applicationStorageDirectory instead.

Related

Playing local HTML5 audio from an external server on desktop

My problem corresponds with the following hypothetical situation:
I have a website with a blog, which stores a playlist of music (just the filenames). I can edit this playlist remotely, for example from my phone when I am on the move. The content of the website is stored in a database on the server (MySQL), which cannot be accessed remotely.
When I get home and I am writing on my blog, I want to play the files in the list, on the same website, by using HTML5 audio. The files are located on my local computer at home. Hence I want to access these local files through my website.
An example of how I am addressing a local file is file:///M:/music.mp3.
The whole set-up works if I work from localhost, so I don't think it is a coding issue.
The problem is that both Firefox and Chrome, my favourite browsers, do not allow third party websites to access my local files without my active input.
It makes sense that the construction above is prevented by user agents due to security issues. I was hoping to find a solution in the fact that I use browser integrated HTML5 audio; IMO, there would be no security issue since the files that are loaded by HTML5 audio cannot be accessed via DOM, so some proper coding of the browsers would have left some breathing room here.
Some extra conditions:
I am looking for the blog and the music player to remain integrated.
I don't want to store my music files remotely.
I don't want to set up a local server.
Perhaps there is a way to add a security exception for my website to my browser, but this seems no longer the case for Firefox.
Any suggestion is most welcome!
I would suggest taking a look at the File System API. It's a very new API that's currently only working with chrome unfortunately :/
What you can basically do is request for access to the local filesystem using window.requestFileSystem() or window.webkitRequestFileSystem(), which has a success callback with a FileSystem object. Using this objects root (a Directory Entry object) you can look up the file using root.getFile(). This has a callback with a FileEntry object. You can then use this file entry to get the actual File object using fileEntry.file() and pass that into a FileReader object to get the data url. Untested example below, probably not perfect and you probably have to tweak with webkit prefixes.
// when file system loads
function onFs(fs){
// locate file
fs.root.getFile('M:/music.mp3', {/*options*/}, function(fileEntry){
// get file / blob
fileEntry.file(function(file) {
// read file
var reader = new FileReader();
reader.onload = function(event) {
var dataUrl = event.target.result;
// create new audio object with data url
// e.g. <audio src="dataUrl" />
};
reader.readAsDataURL(file);
// handle error
} , onError);
// handle error
}, onError);
}
// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, onFS, onError);
(I'd like to also mention, I've never used the File System API, but it seems promising)

AS3 Flash - Save JPG

Im making a little game with in the end the option to take a picture of you to share. Ive succeeded in coding parts like displaying the foto on a bitmap and to save the foto.
Now the problem is that you get a save-as pop-up screen to select where you want to save your foto. But i dont want that option, i just want to save it at a specific folder on my pc. Ive come across similar questions here, to save png/jpg foto's, but none have my specific question.
Is is possible to do?
Thanks,
The.Jack
You can't save directly without a dialogue-box in web, it always asks where it should save. Because it should triggered by user due to flash security issues
But in Adobe Air it is possible to save with flash.filesystem.File and flash.filesystem.FileStream
var fileStream : FileStream = new FileStream();
var file : File = File.desktopDirectory.resolvePath('sample.jpg');
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(defined_byte_array);
fileStream.close();
Hope it helps

Using RequestURL to get to a different folder

I am using AS3 to make a game. I use RequestURL( filename ) to obtain the music I want to play. However, this means the music file has to be in the same folder as the .fla file. How can I use it to get a music file from a different folder? I don't really like having to leave my music outside of a Music folder or properly organised somehow....
EDIT: Should have mentioned this earlier. I tried to use simply something such as "whatever/filename", but I got this security error:
SecurityError: Error #2000: No active security context.
Why?
Have you tried using the flash.media.Sound.load() method yet?
//Make a new sound object
var s:Sound = new Sound();
//Load it
s.load(new URLRequest("someDifferentMusicFolder/loop.mp3"));
//Play it
s.play()
If you are loading it via another method, such as URLLoader + URLRequest, please post your code and we should be able to help a little better.
Generally speaking though URLRequests should allow you to specify different folders, so your problem may not be in the methodology you are using but may be in your implementation? Perhaps you are getting a security error? Whatever it may be, if you post your code we can help you narrow it down.

write/save xml in as3

I want to create a stand alone pure flash application with external xml loading from the same dir. In that i want to update/overwirite my xml nodes and save it internally at run time (I dont want to go for fileReference class to save/overWrite my xml). when I load my apps next time it will react based on the updated xml.
I dont want go for AIR as it requires installation at end user side.
I cant use any server side script.
Is it possible in flash AS3.0?
Please suggest any possible solution.
Thanks..
it's possible with FileReference.save()
here is an example saving .txt files
var file:File = File.userDirectory.resolvePath("test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTFBytes("text to save");
fileStream.close();
last time I tried this worked without any prompt as to where to save . . . if it helps anyone.
(above worked with mobile devices saving to SDcard) - you will need to allow (WRITE_EXTERNAL_STORAGE) permission.
You can't access files in Flash without AIR and FileReference, but you can save data in SharedObject. This is analogous to cookies in browser (but not tied to one). Data will be saved somewhere in "C:\Users\UserName\Application Data\Macromedia\Flash Player\" directory (example for Win7) until user clears it or available disk size is exceeded.

Creating an external .txt in as3

how do I automatically create an external .txt file without asking the user for directory. I am using the following code so as to create and save the ".txt "file, but my code asks for the directory to save the file. I want the file to be saved automatically without asking user for the directory...
import flash.net.FileReference;
import flash.events.Event;
addEventListener(Event.ENTER_FRAME,saveFile);
var ss:String = "this is text to write";
var fileRef:FileReference;
function saveFile(event:Event):void
{
fileRef=new FileReference();
fileRef.save(ss,"save.txt");
removeEventListener(Event.ENTER_FRAME, saveFile);
}
Are you creating an AIR appplication for desktop? If so, you can use File and FileStream.
var f:File=new File("path\to\file.txt");
var str:FileStream=new FileStream();
str.open(f, FileMode.WRITE);
str.writeUTFBytes(contents);
str.close();
If you are creating a flash-player application, then you must ask the user for a path to save (because an online app saving without a user's knowledge is just WRONG)
You cannot do that for security reasons if it's a browser app... Maybe you can try using SharedObjects instead...
is definitely a security issue - saving files without the user knowing what you're doing just isn't right. AIR is the exception - as a "normal" appliaction it can save a file without asking where to put it.
If you are running in a browser, you CAN save to a file, you have to use external interface and be on file:/// protocol and use something like jQuery.twFile.js
Can be done if you compile to AIR (Desktop app). But cannot be done with browser. As i think, it will go against browser's security. Just think, if it were possible, then websites would start storing and loading content on your system without your permission. :)