Skip to main content
Participant
September 2, 2012
Question

How to write text file in Android using Action script 3

  • September 2, 2012
  • 9 replies
  • 4201 views

Dear

I am writing simple program under LG Optimus which is installed Android 2_3(Gingerbread) . But I cannot access text file or xml file. I am using flash professional CS5_5. Can you check the program? What's wrong with it? It works good in PC when change the path, but  not work in Android phone.

----------program---------------


import flash.filesystem.*;
import flash.filesystem.FileStream;
import flash.events.Event;

//txtFld is standard textField component

//btnSaveFile  is standard button component

txtFld.text = "Start";
btnSaveFile.addEventListener(MouseEvent.CLICK,handlerBtnSaveFile);
function handlerBtnSaveFile(e:Event){
  var f:File = new File();
  f.nativePath = "/mnt/sdcard/asdf.txt";
  //f.nativePath.replace(/\//g, File.separator);
 
  var stream:FileStream = new FileStream();
  stream.open(f, FileMode.WRITE);
  txtFld.text = f.nativePath;
 
  stream.writeUTFBytes("This is my text file.");
  stream.close();
   
}

This topic has been closed for replies.

9 replies

Colin Holgate
Inspiring
September 2, 2012

I believe it is recommended that you use resolvePath instead of directly addressing the location. See this article:

http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7fe4.html

That would then make sure it works even if the user doesn't have an SD Card installed. If you do use the SD card, make sure you have included permissions for external storage.

misawoodAuthor
Participant
September 2, 2012

I tried but it work in PC not in phone.  I can not make work. So I conclude that the phone might close file accessing. I wil contact phone maker.  Thank you.

-----------program--------------

import flash.filesystem.*;
import flash.filesystem.FileStream;
import flash.events.Event;

txtFld.text = "Start";
btnSaveFile.addEventListener(MouseEvent.CLICK,handlerBtnSaveFile);
function handlerBtnSaveFile(e:Event){
txtFld.text = "Pressed";
//var f:File = new File();
//var f:File = File.desktopDirectory;
//f = f.resolvePath("asdf2.txt");
var f:File = File.desktopDirectory.resolvePath("Test.txt");
//f.nativePath = "/mnt/sdcard/asdf.txt";
 

var stream:FileStream = new FileStream();
stream.open(f, FileMode.WRITE);
txtFld.text = f.nativePath;
stream.writeUTFBytes("This is my text file.");
stream.close();
     
}

markerline
Inspiring
September 2, 2012

also be sure to check the ActionScript 3.0 Reference documentation for FileStream (put it in the search box and hit enter).  That is how I concluded in the first place to check if FileMode.READ would work.  There might be some other goodies in that document for you to try before contacting the phone manufacturer.

markerline
Inspiring
September 2, 2012

have you tried:

"stream.open(f, FileMode.READ)"

? (without the quotes)

misawoodAuthor
Participant
September 2, 2012

Dear

I tried reading process. The result is good. First, I copy asdf.txt file into sdcard  then tried program. Here is the code.

--------------program--------------

import flash.filesystem.*;
import flash.filesystem.FileStream;
import flash.events.Event;

txtFld.text = "Start";
btnSaveFile.addEventListener(MouseEvent.CLICK,handlerBtnSaveFile);
function handlerBtnSaveFile(e:Event){
  var f:File = new File();
  f.nativePath = "/mnt/sdcard/asdf.txt";
  //f.nativePath.replace(/\//g, File.separator);
 
  var stream:FileStream = new FileStream();
  //stream.open(f, FileMode.WRITE);
  stream.open(f, FileMode.READ);
  var strFileData:String = stream.readUTFBytes(stream.bytesAvailable);
  //txtFld.text = f.nativePath;
  txtFld.text = strFileData;
 
  //stream.writeUTFBytes("This is my text file.");
  stream.close();
   
}