Does phonegap have access to the Android DownloadManager class? - android

I have successfully downloaded a file using the phonegap download method. I am downloading the file to local storage in the Download folder on my android phone. I would like to have some kind of notification from the Download Manager that a file has been downloaded and then click on the download to view. This is what the phonegap download method lacks.
Does phonegap have access to the Android DownloadManager class?

You definitely need a plugin to access native code of android from phonegap.
Have a look at this site: https://github.com/phonegap/phonegap-plugins/tree/master/Android
It is full with plugins. One of them is called "downloader". Maybe that is what you are looking for.

The android downloader plugin works great. However if you are running on cordova 2.0 you will need to modify the code for adding a constructor. The current code is:
...
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("downloader", new Downloader());
PluginManager.addService("Downloader", "com.phonegap.plugins.downloader.Downloader");
});
Since addPlugin no longer works on cordova 2.0 you will need to remove the addConstructor method and replace it with:
window.downloader = new Downloader();
And on the exec method should look like this:
cordova.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]);
Instead of using the Phonegap object. After I did all this, the plugin worked. The entire js should look like this:
function Downloader() {}
Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) {
//Make params hash optional.
if (!fail) win = params;
cordova.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]);
};
window.downloader = new Downloader();

use this plugin to access android DownloadManager
https://github.com/sgrebnov/cordova-plugin-background-download
i use it in my cordova app and it work fine.
maybe need some customization in java code to work perfect.

Related

How to download zip file from url in unity?

I am using unity 2019.2.14f1 and visual studio 2019.I also tried with ICSharpCode.SharpZipLib but can't find any solution.I am trying with the code from the code which is available in the following url:
Unzip a memorystream (Contains the zip file) and get the files
Thanks in advance.
You can download files in Unity using UnityWebRequest.Get. Note that using it like in the example shown on that page, you're going to want to make sure you know how to use Unity's Coroutines.
Also, instead of downloadHandler.text you're going to want to use downloadHandler.data to get the downloaded data as bytes you can put into a stream.
Once you've got those you can use the appropriate libraries or standard library calls to unzip your files.
To download a file from a URL you can use system.net
WebClient client = new WebClient();
client.DownloadFile("http://whateverlinkyouhave.zip", #"C:\whateverlinkyouhave.zip")
Everyone's answer is correct, especially using UnityWebRequest. One of things you must do is to turn the byte[] data to zip file. And then use some Unzip library.
Example implementation
using System;
using UnityEngine.Networking;
private IEnumerator DownloadZipFile(string url)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
string savePath = string savePath = string.Format("{0}/{1}.zip", Application.persistentDataPath, "zip_file_name");
System.IO.File.WriteAllBytes(savePath, www.downloadHandler.data);
}
}
}
Hope it helps.

fileSystem does not work as before

I used the code below to get the path to download, but once formatted and installed the mac version 3.3.0rc1 of the cordova, the code does not work as it should.
function onSuccess(fileSystem) {
console.log(fileSystem.root.fullPath);
}
// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);
Before, the code returning something like:
/Users/myuser/Library/Application Support/iPhone Simulator/5.0/Applications/CF2A9018-49B9-4DE6-91FC-EA76CB435FC8/Document
Now, the code returns only:
"/"
And I can not download that way
Any change in the cordova? What code should I use to download?
I've tried several but did not succeed.
Thanks to all.

Flex Air Mobile File Download

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")
}
}

AS3 Air On Android - Load SWF as its own Main()

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
}

Premade APK template for bundling static html website on Android?

I have a 3MB website that's static HTML/CSS/JS and I'd like to package it up into an Android app for offline use.
Are there premade APK archives for static webapps... the kind where I can just insert the static html/css/js and the app is programmed to load "index.html" or something? Presumablty I'd need to put index.html in /res/raw or /assets. I'm not a Java programmer so that's that main reason I'm asking.
I understand that APK files need to be cryptographically signed but presumably I can do this later.
AppMobi takes your webapp, compiles it with their pre-built template, and gives you the resulting APK. Make sure you use a custom loading splash image and implement the hack workaround in the forums to speed up first-time loading.
You don't need to do anything but create a new project and paste your web-app into the project folder, then follow the screenshots. And that hack I mentioned.
If you have or understand gradle, I did this to prevent polluting the actual web-app:
task appmobi(dependsOn: [buildForWeb]) << {
File indexFile = new File('build/index.html')
def appMobiLoader = '''<script type="text/javascript" src="http://localhost:58888/_appMobi/appmobi.js"></script>
<script type="text/javascript">
function deviceReadyListener() {
AppMobi.device.hideSplashScreen();
}
window.document.addEventListener("appMobi.device.ready", deviceReadyListener, false);
</script>'''
def indexText = indexFile.text.replace("</head>", "$appMobiLoader</head>")
indexFile.withWriter { writer ->
writer.write(indexText)
}
}
Check out the PhoneGap project. http://www.phonegap.com/

Categories

Resources