开发者

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.开发者_Go百科 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.


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 .


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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜