hello evrey body i have try really hard to write a file to android and so far no secceus.
this is the code i am using.
function writeAirFile():void
{
// Change the folder path to whatever you want plus name your mp3
// If the folder or folders does not exist it will create it.
var file:File = new File("/mnt/sdcard/new/new.apk");
//file.nativePath = "/mnt/sdcard/new/new.apk";
trace(file.nativePath);
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
trace("The file is written.");
trace(file.nativePath);
}
uts need to work but it is not.
i already add th permission for local storge writing
please i need help
thanks a lot for all of you for helping!
Point out custom directory is not best practices better you can use File.applicationStorageDirectory so no need worry about security and permission related issue.
var file:File = File.applicationStorageDirectory.resolvePath("new/new.apk");
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
trace("The file is written.");
trace(file.nativePath); // you can find where it is stored.
Suggestion always use Async mode file operation so that UI freeze won't happen even if file size is too large.
Related
In my app, I need to load some external swf files. I used following code:
var file:File;
file = File.documentsDirectory.resolvePath("myfolder/myfile.swf");
if(file.exists)
{
var inFileStream:FileStream = new FileStream();
inFileStream.open(file, FileMode.READ);
var swfBytes:ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
var loaderContext:LoaderContext = new LoaderContext(false, new ApplicationDomain(null));
loaderContext.allowLoadBytesCodeExecution = true;
loaderContext.allowCodeImport = true;
myLoader = new Loader();
try
{
myLoader.loadBytes(swfBytes, loaderContext);
}
catch(e:Error)
{
trace("Can't read file.");
}
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete_swf);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop, false, 0, true);
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadingError);
}
else
{
trace("File doesn't exists.");
}
Everything works fine. But I recently found out some of my swf files are corrupted. When loading those files, it doesn't dispatch complete event and it also doesn't throw any error. So, my question is, is there any way to find if swf file is corrupted or not? Yes I can replace those corrupt swf files. This is just a precaution if this kind of problem occurs again. At the moment, all I can think of is to create a timer and check if complete event is dispatch or not. If not then show can't read file message. Is there any better way of dealing with this?
When a file is corrupt all sort of unexpected things might happen. Setting a timeout is a good way to handle no events loading issues. As suggested by #seventeen you can use the crypto library to verify file hash, but this requires that you already have a hash to compare to, and that the load complete event is fired.
I'm downloading some images into my application so they can be displayed without consuming any bandwidth. The code I'm using to store the images on my android device is the following:
var file:File = File.applicationDirectory.resolvePath(fileName);
if (file.exists)
{
file.deleteFile(); //delete it if exists
}
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(data, 0, data.length);
fileStream.close();
var showLocalPath:String = file.nativePath;
To display the images I'm using the following two examples:
<s:BitmapImage id="iconStateImage" source="{appContext.CurrentState.IconImage}"/>
<s:Image id="backgroundStateImage" left="0" right="0" top="0" bottom="0" source="{File.applicationDirectory.resolvePath(appContext.CurrentState.BackgroundImage)}"/>
Both appContext.CurrentState.IconImage and appContext.CurrentState.BackgroundImage have the same path of the former fileName. Even though I'm trying to load the images with full path (second case) or with a short path (first one), I'm not able to display it.
I already set the WRITTING permissions on the project.
Can some help me with this? Thanks in advance.
Regards,
Sebastián
File.applicationDirectory is read-only; you'll want to use File.applicationStorageDirectory instead.
See also How can you delete a file in the application directory? and the Flex File documentation.
Then add "app-storage:/" before the image path:
<s:Image id="backgroundStateImage" source="app-storage:/{appContext.CurrentState.BackgroundImage}"/>
I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).
I try to save in png file format. It dose't work when i try to publish to android phone. i do not understand how android phone path from folder to folder work. how do i make it work...
I try the same Actionscript 3 and publish it on my pc and it work....
pls help. sorry bad grammar.
function export():void
{
var bmd:BitmapData = new BitmapData(480, 800);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd);
var file:FileReference = new FileReference();
file.save(ba, "MyDrawing.png");
}
This is the way i save images on Android:
var file:File = File.applicationStorageDirectory.resolvePath("MyDrawing.png");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeBytes(ba, 0, ba.length);
stream.close();
why you wants to save your drawing in phone memory ?How can u tackle when memory size exceeds ?
Anyway Refer this Link,
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
I want to download a set of video files to a default folder path in an Android device and then have access to the path and files. The FileReference class does not support this features. Is there another way? Thank you.
I dont use file reference at all. I use a Loader to download the file. I then create a new File object with the path where I want to save the file, and use a FileSteam to write the data from the loader to the file.
var req:URLRequest = new URLRequest(url);
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handler);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(req);
function handler(e:Event):void{
var file:File = new File(path);
var stream:FileSteam();
steam.open(file, FileMode.WRITE);
steam.writeBytes(loader.data);
steam.close();
}
something like this should accomplish what you are looking for. I wrote this code from memory so it may not be exactly correct but should be good enough to get you going. Make sure you put this in a try/catch since file and filesteam both throw exceptions