Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading files on Android

I'm in the middle of writing an Android app, and would like to give my users the ability to share the document files it creates.

What I'd ideally like to see would be files hosted on a HTTP server somewhere, so a user can simply fire up their browser on the Android phone, surf to the relevant page, and then download the file to their phone. I'd then like for my app to be able to open that downloaded file.

I'm not sure if that's possible at all, but would certainly be interested in hearing from anyone that knows anything about such things. Unfortunately I seem to be having difficulty coming up with the answers myself - like much of the rest of the Android SDK, there is a severe shortage of relevant documentation.

like image 960
Mac Avatar asked Dec 23 '09 23:12

Mac


3 Answers

When the user access a file you wish to support you should be able to register with Android using IntentFilters to indicate that your application is able to handle a specific MIME-TYPE.

See the documentation here .

like image 118
Scott Markwell Avatar answered Sep 18 '22 18:09

Scott Markwell


It's easy enough to get files from a web server. In your includes -

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;

In your code -

URL requestURL = new URL(urlStringForFileYouWantToOpen);
URLConnection connection = requestURL.openConnection();
InputStream response = connection.getInputStream();

Then do what you want with the InputStream. I'm not sure if that's close enough to your description of the user downloading a file and then accessing it from your app, but it seems more straightforward to me to let them get the file while in your app.

like image 20
jball Avatar answered Sep 17 '22 18:09

jball


Incidentally, I've stumbled across a reasonable solution that is a fair bit easier than messing around with intents and so on...

The WebView widget allows you to set a DownloadListener object that gets notified whenever the WebView is directed to a file of a type it doesn't natively understand. Thus, the functionality I was after can be achieved by creating a WebView in my application and registering a DownloadListener to listen for when the user downloads one of my application's document files.

Thanks for all your help!

like image 21
Mac Avatar answered Sep 17 '22 18:09

Mac