How to download files from web and save them on the device - android

I've got a link for a media file on the web that my application usually plays using a MediaPlayer. I want the user to be able to download the file and save it on his device.
How can I download the file and save it on a predefined folder on his device.
I'm actually asking for two things
1) How to download a file
2) How to save a file to the device's memory.

You may want to do HTTP GET to get a file, start looking at HttpClient class and example, to save, data storage.

You've asked a very general question, so the best I can do is give you a general answer. To actually download the file itself, you'll probably want to use an HttpURLConnection. Make sure not to do this on the main thread UI thread as it will cause your app to freeze. You'll want to do it on separate thread using something like an AsyncTask. To actually have the user kick off the download, you'll probably want to put some sort of button in your UI that allows them to first select a folder and then once a folder is selected, you launch your AsycnTask. Android doesn't really have a builtin file manager, but you can could integrate with the OpenIntents file manager.

Related

using html as front end and text file as back end can save the data in android

My requirement is doing a project with android, now I developed the project in asp.net and call the URL in android web view. It works fine with the internet connection, but when internet is not available the problem occurs, what I have to do?
using html as front end and text file as back-end can save the data and sync when internet connection available, Is it possible?
or any other easy method to do?
please provide some suggestion.
Yes. You can do what you said. You could have a local copy of the web inside the app assets folder and, for example, you can load it like this:
webView.loadUrl("file:///android_asset/web/index.html");
Then you will have to implement an automatic downloader for the new online version. You could do it by putting a simple txt file with the current version (that you have to compare with the one in the app assets) or you could do a restapi that also handles the download (that's the better way, but it needs more time obviously).
Also, you have to write the files you download (for the new version) to the app internal memory (you could get the folder with "context.getFilesDir()") and then check if there is a version of the webapp inside that folder and if not fall back to the one in assets directory (that's included with the app apk).

Run code during installation or do something during downloading from google play

I need download some images from web (the app will use these images) before first run of the application. During installation or during downloading from Google Play. Is it possible?
I need to use logos in the first Activity, that are put on the web archives. I would not do it, but the app should work without internet connectivity and when I want to download an image during the app running, I will not download it without the internet connection. That´s why I am asking, how to do it another way?
You need to download these images on the first launch of your application. Everytime the application is openned, you need to check if the images are available (in the folder you downloaded them). If not, you need to download them. Be sure to let the user know what you are doing, and make them waiting while you download them.
Other solution would be to include them directly in the .apk (add them in drawables/raw folder, they're meant to be used this way)

Downloading to a temp folder then overwriting original files - speed issues?

I have an app that downloads a single html file and various images and sometime, mp4 videos.
After an initial download, repeat downloads are done every 15 minutes by a background service.
The service checks if there are any new files and if any files have been modified (in this case, it is typically the html file that would be modified and some new images will be downloaded)
I have a webview that is displaying the html file and after the background service successfully downloads some new assets, the webview get refreshed.
However, whilst the service is downloading, the app would be using the files, displaying them in the webview (the videos are handled with a videoview, using a javascript bridge, which flips the two views around)
So, to my question.
After seeing some possible issues with the current app, I want the background service to download the assets to a temporary folder, instead of the main folder used (I use a sub-folder created in Downloads)
Then, once the downloads are complete, I would "stop" the webview, copy the files form "tmp" to the real folder, and restart the webview.
Logic sounds OK, but I am worried about speed. The files to copy could weight in at 100mb potentially (maybe more, hopefully less, due to bandwidth issues obv.) so how fast could Android copy those over?
Ideally the transition needs to happen in under a couple of seconds.
Can anyone advise on this?
Is there possibly a better way to handle this situation?
Put the temporary directory below the main data directory, then use an atomic rename(2) call to move the new file into place. (This only works atomically when the source and destination directories are within the same filesystem, hence the recommendation about tempfile placement. You can also put them as FILENAME.EXTENSION.new into the same directory then rename them. Be wary of tempfile races, as usual, when designing filenames; use something like mkstemp(3) to create them if you can.)
Could you point the WebView at the "temporary" directory instead, and then in the background delete the original directory? (then you'd always use the "temporary" directory going forward).

android downloading files

I have an application that will mail users attachments of text files. Receiving users can upload the file using this application. My question is
1) Is download folder the default location for downloading files?
2) Is it necessary for me to provide a folder exploring options before the files can be uploaded?
Any suggestions?
Jai
1)It depends on your actual call and how you are receiving the data / storing it. From what I've seen it will download to one static location however it's probably not a good idea to lean on that. There is always a possibility for that file to be moved by the user which could result in a crash or strange errors.
2)I would look into that or having the application create a temp folder to store the aforementioned text file in.
Good luck!
!k

Downloading resource files in Android

I've been trying to make a game, and I'm getting the background music to work right now. The files are simply too large to fit in the res/raw directory and compile into an apk, so I'm downloading them from online. This is a good solution; it doesn't use up any space on the phone and it plays smoothly, just that it won't work if the phone can't connect to the internet when the app is running. A better solution would be to download the music when the app starts for the first time, but I can't find the documentation on how to do this.
Any help would be appreciated.
A better solution would be to download the music when the app starts for the first time, but I can't find the documentation on how to do this.
There are lots of documentation of how to download a file. So, if you want to know when the app starts the first time you can do it using SharedPreferences (here is an example).
Also, you can check if the files are there before trying to download them:
File theFile = new File("/sdcard/blah");
if(!theFile.exists(){
// if it does not exists it means that it has not been downloaded
// or maybe the user delete it
}

Categories

Resources