This code works great in a desktop air application but doesn't work at all in android. is this not possible yet in air for android? here's my code.
var theImage:BitmapData = new BitmapData(imageArea.width,imageArea.height);
theImage.draw(imageArea);
var jpgStream:ByteArray = theEncoder.encode(theImage);
var fileName:String = "pf_" + int(Math.random() * 10000) + ".jpg";
while(File.userDirectory.resolvePath("DCIM/Camera/" + fileName).exists)
fileName = "pf_" + int(Math.random() * 10000) + ".jpg";
trace(fileName);
var fl:File = File.userDirectory.resolvePath("DCIM/Camera/" + fileName);
var fs:FileStream = new FileStream();
trace(fl.url);
try{
fs.open(fl,FileMode.WRITE);
fs.writeBytes(jpgStream);
fs.close();
}catch(e:Error){
trace(e.message);
}
I get this trace on the fl.url
file:///mnt/sdcard/DCIM/Camera/pf_2570.jpg
image area is a flex group with several displayed children. Thanks
I didn't try to debug your code, but what you are doing should work. It is certainly possible to write to files with AIR on Android. Are you sure jpgStream has data?
Did you add the android permission to write content on SD card? ( or external storage )
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Related
Currently, I'm trying to download a file I have in a bucket on Amazon s3. I'm using this code to debug, because I always get an EN0ENT / file not found when I try to read from the file in the end.
String str_FilePathInDevice = "/sdcard/" + "/"
+ "RestoreFolderName" + "/" + "filname.extention";
File file = new File(str_FilePathInDevice);
String str_Path = file.getPath().replace(file.getName(), "");
File filedir = new File(str_Path);
try {
filedir.mkdirs();
file.createNewFile();
} catch (Exception ex1) {
}
System.out.println(file.toString());
System.out.println(file.canRead());
System.out.println(file.length());
TransferObserver observer2 = transferUtility.download(
"arabianbucket", /* The bucket to upload to */
"demo.txt", /* The key for the uploaded object */
file /* The file where the data to upload exists*/
);
System.out.println(file.toString());
System.out.println(file.canRead());
System.out.println(file.length());
the file.canread() ends up being flipped from true to false after the transferutility.download link. I'm not particularly sure why this is the case. Does anyone know how to successfully read from an object in Amazon s3? In my manifest, I've already flipped the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Thank you!
TransferUtility offers asynchronous transfers. In your code transferUtility.download initiates a download and the download happens in a background thread so as not to block the main thread. I don't recommend any file operation until the transfer is done. You can attach a listener to the observer and listens to status and progress changes. See Store and Retrieve Files with Amazon S3 for more information.
I have a problem with Android and Unity 3D. I have a file read code. When I put my code on the computer, it works. However, my code does not work on Android (Mobile). How can I solve this problem? Thank you.
FileInfo theSourceFile = new FileInfo(filename);
if (System.IO.File.Exists(fname))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
EDIT updated code
string filename = "file.txt";
FileInfo theSourceFile = new FileInfo(filename);
filename = Application.persistentDataPath + "/"+filename;
System.IO.File.WriteAllText(filename,"Test");
if (System.IO.File.Exists(filename))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
You need to change your build settings for android Device.
Change Configuration >> write access to external (sd card).
if not, your app is pointing to internal path and you need root permission in your android device.
You must use Application.persistentDataPath on Android to be able to read anything.
Change that to string filename = Application.persistentDataPath + "/file.txt"; and your code should work fine.
Bear in mind that before the read function can work, you must write to the directory first. So file.txt must exist in Application.persistentDataPath first.
For example
string filename = Application.persistentDataPath + "/file.txt";
System.IO.File.WriteAllText(filename,"Test");
EDIT:
You new code is still not working because you had FileInfo theSourceFile = new FileInfo(filename); before filename = Application.persistentDataPath + "/"+filename;. This means that the file name is still not valid. Pay attention the order your script execute. After switching it, it worked on my Android. Below is the whole code.
string filename = "file.txt";
filename = Application.persistentDataPath + "/" + filename;
System.IO.FileInfo theSourceFile = new System.IO.FileInfo(filename);
System.IO.File.WriteAllText(filename, "Test");
if (System.IO.File.Exists(filename))
{
System.IO.StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(text);
}
i want to open a .mp4 file, so that i can access each frame.
I tried to do it using javacv, but it didnt work.(see code below)
Does anyone know why it didnt work or how else i could achieve my goal?
i tried to call
string path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "Test";
opencv_videoio.VideoCapture cap = new opencv_videoio.VideoCapture(path + File.separator + "test.mp4");
but then cap.isOpend() returns false. I have set the permission WRITE_EXTERNAL_STORAGE(which includes READ)
i can read and write file with this method for storing game progres.
filePath = Application.dataPath + "/";
filenam= "SavedGame";
extension = ".txt";
Lettura ----------------
var FRead = new File.OpenText(filePath + fileNam + extension);
for (var i : int= 0; i<N; i++) {
FRead.ReadLine();
}
Scrittura--------------------
var FWrite: StreamWriter = new StreamWriter(filePath + filenam + extension);
FWrite.WriteLine("AAAA");
A run time in the Unity editor all work perfectly.
But in Android device Application.dataPath seems dont work.
I tryed Application.persistentDataPath that work fine but in (Read Only),but i need place the SavedGame.txt manually in the patch.
I tried a fantomatic "jar:file://" + Application.dataPath + "!/assets/"; but dont work.
Note when i build the project in apk packege semms dont appear my SavedGame.txt,that
in the editor is in UnityProject\Assets .
Come posso risalire al percorso giusto del mio SavedGame.txt, posizionato in ad esempio in UnityProject\Assets nei device?
How i cand do this in a correct way read and write SavedGame.txt in android device?
On android you might not have direct write access to the dataPath of the project.
Try using Application.persistentDataPath instead. This is where save files are intended to be stored.
Change:
filePath = Application.dataPath + "/";
to
filePath = Application.persistentDataPath + "\\";
I am making an android application in Air for android using actionscript 3. I need to download a file from the server and save it to mobile sdcard. Following is my code, the file gets loaded from the server but how do I save it on sdcard?
import flash.filesystem.*;
var urlString:String = "http://www.mywebsite.com/xyz/myswftodownload.swf";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(ProgressEvent.PROGRESS, onProgress);
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void
{
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
trace("Loaded");
urlStream.removeEventListener(ProgressEvent.PROGRESS, onProgress);
urlStream.removeEventListener(Event.COMPLETE, loaded);
//NOW HOW TO SAVE THE LOADED FILE ON SDCARD??
}
function onProgress(evt:ProgressEvent):void {
var nPercent:Number = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
//loadingAnim.bar.scaleX = nPercent / 100;
status.text = nPercent.toString() + "%";
}
You can do something like this
//NOW HOW TO SAVE THE LOADED FILE ON SDCARD
var file:File = File.userDirectory.resolvePath("myswftodownload.swf");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
File.userDirectory usually points to the sdcard (external storage). But beware of the fact that external storage doesn't have to be available at all time (for example when device connected as mass storage by usb).
In your case i would consider using internal storage instead and use File.applicationDirectory or File.applicationStorageDirectory for saving your swf files. External storage is better for saving files like images, music, documents..