I am building an Adobe Air Mobile application currently targeting Android (and eventually iOS). I'm using Flash Builder 4.6 (which is Flex).
I have a requirement to download a zip file from a url, save it to local storage and unzip the contents.
There seem to be a few example on how to do a download in Flex/Air, but not many in Flex/Air/Mobile. Additionally, the mix of examples seem to be all over the place with namespace versions, etc.
Could someone please provide a succinct example on how to do this in an Air Mobile app?
Thank you!
Edit: What I've tried:
So, from the examples on the web, the first thing I did was add namespace in Flex declarations: xmlns:net="flash.net.*"
Then I added the following component: <net:URLLoader id="urlLoader" />
Then, in the button click event, I called it: urlLoader.load(new URLRequest(downloadUrl));
I get a run time error: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http://...
The problem is I don't know where to go from here since I'm not sure what's missing.
this code works for me on IOS:
public function download():void{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
loader.addEventListener(Event.COMPLETE,downloadComplete);
loader.load(new URLRequest(YOUR_URL));
}
private function downloadComplete(event:Event):void{
try{
var file:File=File.applicationStorageDirectory.resolvePath(YOUR_LOCAL_PATH);
var ba:ByteArray = event.target.data as ByteArray;
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(ba);
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.close();
Alert.show("File downloaded succesfully")
}
catch(eeee){
Alert.show("error")
}
}
Related
how load local html on air sdk harman, i use StageWebView on AS3, if i run on its normal view, but if i make apk and instal on android phone, its error. Application cannot showing local html. I use air SDK harman 33.1.1.554.
this my code :
var file:File = File.applicationDirectory.resolvePath("html");
var destinasi:File = File.applicationStorageDirectory;
file.copyTo(destinasi, true);
var hasil:String = "file://" + destinasi.resolvePath("index.html").nativePath;
isiMenu4.loadURL(hasil);
is there issue about security on air sdk by harman? how to skip that?
This might be an issue where you don't have Android permission to write to local storage.
Try creating the directory first and see if that throws any errors.
_file = File.applicationStorageDirectory.resolvePath("html");
trace("html.url", _file.url);
trace("html.nativePath", _file.nativePath);
if (!_file.exists) {
_file.createDirectory();
//make storage directory non backupable
_file.preventBackup = true;
}
I am developing a NativeScript Android app and using QuickBlox.
I can open images and audio files inside the app, but there are some issues with .pdf, Word and Excel documents.
Code written by a co-worker:
var attID = message.attachments[0].id
var fileSrc = ChatManager.getQB().content.publicUrl(attID) + "/" + "/download.xml?token=" + ChatManager.getSessionToken()
I get the URL with blob + session token, then:
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(args.object.src));
intent.addCategory(android.content.Intent.CATEGORY_BROWSABLE);
application.android.startActivity.startActivity(intent);
In this way I successfully download .pdf files, but .doc, .docx, .xls and .xlsx return without extension.
I also tried getting the URL through privateUrl() (without interpolating the token), with the same result.
Another not working method:
httpModule.request({
url: uid,
method: 'GET',
headers: {
'QuickBlox-REST-API-Version': '0.1.0',
'QB-Token': ChatManager.getSessionToken()
}
}).then(res => {
var file = res.content.toFile();
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
var uri = android.net.Uri.fromFile(new java.io.File(file.path));
intent.setDataAndType(uri, 'application/pdf');
application.android.startActivity.startActivity(android.content.Intent.createChooser(intent, 'Apri file...'));
});
Any way I try, it feels like QuickBlox returns the extension on .pdf files only. Any suggestions?
UPDATE:
I tried opening the URL on different devices.
https://api.quickblox.com/blobs/[blobId]?token=[token]
On Chrome for Windows and iOS Safari, the file is downloaded or opened in browser correctly. On Android, it returns without extension on these browsers: Chrome e LineageOS stock browser. On Internet Samsung 7.4.00.70, it's opened in the browser correctly.
With httpModule.request(), I get this warning in the debug console on result:
Resource interpreted as Document but transferred with MIME type [my file mime type]
At the moment I am using this workaround in native code, but later I'll need to find something that works on iOS too.
I'm not marking this as an answer since it doesn't solve the problem
var r = new android.app.DownloadManager.Request(android.net.Uri.parse(args.object.src));
r.setDestinationInExternalPublicDir(android.os.Environment.DIRECTORY_DOWNLOADS, fileName.text);
r.allowScanningByMediaScanner();
r.setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
var dm = application.android.startActivity.getSystemService(android.content.Context.DOWNLOAD_SERVICE);
dm.enqueue(r);
I have simple video playing apps both set up by PhoneGap and Chrome Apps CLI's (Both use Cordova), they contain a bunch of short educational videos and are needed as both website and application on Android/iOS for offline usage.
I have found, so far, that the total size of the Chrome Apps bundled file can't exceed 10mb and the PhoneGap Build can't exceed 40mb - so both will need to download and store files locally for later use.
The videos will need to open and play from within the WebView browser - hotspots trigger JS that change the HTML5 video src. (AppCache and other HTML5 storage are out the question for mobile devices, they never seem to be able to reach triple digit storage space)
Has anyone had luck with a certain Cordova/PhoneGap/Chrome App API that can store files locally to achieve this spec?
Any advice/help/pointing in right direction appreciated!
You can do this in Cordova apps (and very soon in Chrome Cordova apps). You'll need the most recent versions of the File (1.0.1) and FileTransfer (0.4.2) plugins.
With those, you can use FileTransfer.download() to download the video, and you can use File to access the file and create a <video> tag to play the video.
You'll want to use the .toNativeURL() method on the file entries before you play them. Recent versions of the File plugin use a custom URL scheme for files, which is unfortunately not compatible with the HTML <video> tag.
This is the test code that I use to test the interaction of these methods:
var filename = "small.mp4";
var videoURL = "http://techslides.com/demos/sample-videos/small.mp4";
requestFileSystem(PERSISTENT, 0, function(fileSystem) {
var ft = new FileTransfer();
ft.download(videoURL, fileSystem.root.toURL() + "/" + filename, function(entry) {
var videoElement = document.createElement('video');
videoElement.controls = 'controls';
videoElement.src = entry.toNativeURL();
document.videoElementById("output").appendChild(imgElement);
});
});
Update
With the latest version of the File plugin (1.1.0), you no longer need to use .toNativeURL() to obtain a URL that you can use as a src attribute for a video. The standard .toURL() method will return such a URL.
Here is the code to download file using phonegap filetransfer
function downloadFile(){
window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"test.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry){
var Path = fileEntry.fullPath.replace("test.html","");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download(
yourserverurl,
Path + "yourfilename+extension",
function(theFile) {
window.localStorage.setItem("FilePath", theFile.toURL());
console.log(theFile.toURL());
},
function(error) {
console.log("upload error code: " + error.code);
}
);
},
fail);
},
fail);
}
function fail(error) {
console.log(error.target.error.code);
}
You can store the fileURL in localstorage for further usuage
I have an XML file on the SDCard, that is downloaded from a server. I am trying to open and read in that XML in order to use it within my app. I open and read it fine, but JQuery is freezing on parsing it.
Once I get the file I do like so:
var reader = new FileReader();
reader.onloadend = function (evt) {
parseXML(evt.target.result);
};
reader.readAsText(file);
And then I begin parsing like so:
function parseXML(xml){
$(xml).find('recipe').each(function(){
And it stops there. If I load the in-app XML using JQuery's ajax method it works fine. So, is there anything special about loading XML data using the FileReader in PhoneGap?
UPDATE:
OK, with much time and help from another dev, the issue appears to be a bug in PhoneGap. Specifically the FileTRansfer object.
I was doing this:
var ft = new FileTransfer();
var dlPath = dataDir.fullPath + "/recipes.xml";
ft.download("http://design.mydomain.com/dave/humana/recipes/recipes.xml", dlPath, addedNewRecipes, addError);
And I'd get a trace within addedNewRecipes that the download succeeded. And it did... just not all of it. The file was truncated and that's what was causing the parseXML to fail. The entire XML file is about 90KB (~1600 lines) it was being truncated at maybe 50 lines.
Replaced FileTransfer with a FileWriter object and it works now.
PhoneGap/Cordova really is still beta isn't it.
I'm using a wrapper app to download a remote SWF and save it to a Xoom Tablet. The download and save works fine but using something like
var file:File = File.applicationStorageDirectory.resolvePath("app.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context needed?*/);
_main.addChild(loader);
Screws with the targeting in the loaded swf so that in my loaded swf if I try and call "stage" from "Main" its null. I thought since loaded SWF's had there own sandbox it would load and run just like it I installed it by its self, does not seem to be the case. If this was a desktop app I could use NativeWindow but its not supported on mobile devices yet. Are there any know options where I can load and run it in its own sandbox where paths and roots are intact? Possibly a command it launch it as a separate swf from my wrapper swf?
I ran into more issues but finaly found the correct way, you cant acceess "File" but this works just fine.
function getSWF():void
{
var request:URLRequest = new URLRequest("child.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
loader.load(request);
_main.addChild(loader);// _main is my display sprite
}
function loadHandler(event:Event):void
{
var childSwf:Object = event.target.content;
childSwf.init( PARAMS );// in my case I needed to pass XML but this is any method in the Main of the child swf, note that "stage" is read only, so your parent swf needs to set any stage vars you need
}