views:

569

answers:

4

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.

+2  A: 

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.

jball
+3  A: 

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 .

Scott Markwell
This sounds pretty much like what I'm after. I'm gathering that there will basically be a particular Intent fired when a file is downloaded, and by registering to handle a particular MIME type I can receive that Intent? If so, which Intent in particular are we talking about, and how does one perform that registration? I've had a quick look over the link you provided, but that page is so long it's quite difficult to find anything in particular.
Mac
There are enumerations of types of intent, such as view, edit, etc. I'm not familiar enough with Android yet to describe the step by step, but in you application's manifest file, you include xml of the intent types and data you handle, this will register you with the OS.
Scott Markwell
You may wish to jump to the example section on that pagehttp://developer.android.com/guide/topics/intents/intents-filters.html#npex
Scott Markwell
A: 

If you want to integrate the download from within the app then @jball's answer is what you need if your would prefer that the process is initiated via the browser then @Scott's answer is what you need. From your description it sounds like you also want users to share documents created with the app which would require your app to be able to upload apps to the webserver. Something like WebDAV would be ideal for that, either implemented yourself using org.apache.http library or using one of the open source implementations out their.

Nic Strong
+1  A: 

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!

Mac