简体   繁体   English

从URL下载android 2.2中的5-40 MB数据文件。 开发使用哪些类?

[英]Download data file 5-40 MB in android 2.2 from URL. Which classes to use for development?

I am developing an application in which i need to download a file(.zip / .txt / .jpg etc) size- 5 to 50 MB.. Application based on Android 2.2. 我正在开发一个应用程序,其中我需要下载大小为5到50 MB的文件(.zip / .txt / .jpg等)。基于Android 2.2的应用程序。

The user provides the URL and triggers the download but then the downloading process runs in background until complete. 用户提供URL并触发下载,但是下载过程在后台运行,直到完成。

streaming should be used for downloading file. 应该用于下载文件。
I want to know how can this be done using HTTP connections . 我想知道如何使用HTTP连接来完成。
what classes can be used for this? 什么可以用于此?
Does android 2.2 provides an API for this? android 2.2是否为此提供了API?

Any kind of help is appreciated.... 任何帮助都将受到赞赏。

Android did include an API called DownloadManager for just this purpose...but it was release in 2.3; Android确实为此目的包含了一个名为DownloadManager的API ...但是它是在2.3版本中发布的; so while it won't be useful in your application targeting 2.2, it might still be a good resource for you to research the implementation. 因此,尽管它在以2.2为目标的应用程序中没有用,但对于您研究实现还是可能是一个很好的资源。

A simple implementation I would recommend is something like this: 我建议一个简单的实现是这样的:

  • Use an HttpURLConnection to connect and download the data. 使用HttpURLConnection连接和下载数据。 This will require the INTERNET permission to be declared in your manifest 这将需要在清单中声明INTERNET权限
  • Determine where you want the file to be. 确定您想要文件在哪里。 If you want it on the device's SD card, you will also need the WRITE_EXTERNAL_STORAGE permission. 如果要在设备的SD卡上使用它,则还需要WRITE_EXTERNAL_STORAGE权限。
  • Wrap this operation in the doInBackground() method of an AsyncTask . 将此操作包装在AsyncTask的doInBackground()方法中。 This is a long-running operation, so you need to put it into a background thread, which AsyncTask manages for you. 这是一项长期运行的操作,因此您需要将其放入AsyncTask为您管理的后台线程中。
  • Implement this in a Service so the operation can run protected without the user keeping the an Activity in the foreground. Service实现此功能,这样操作就可以在没有用户将活动保持在前台的情况下受保护地运行。
  • Use NotificationManager to notify the user when the download is complete, which will post a message to their status bar. 下载完成后,使用NotificationManager通知用户,这将在用户的状态栏中发布一条消息。

To simplify things further, if you use IntentService , it will handle the threading for you (everything in onHandleIntent gets called on a background thread) and you can queue up multiple downloads for it to handle one at a time by simply sending multiple Intents to it. 为了进一步简化操作,如果您使用IntentService ,它将为您处理线程( onHandleIntent所有onHandleIntent都会在后台线程上调用),并且您可以将多个下载排队,一次只需向其发送多个Intent即可处理一次。 Here's a skeleton example of what I'm saying: 这是我所说的最基本的例子:

public class DownloadService extends IntentService {

public static final String EXTRA_URL = "extra_url";
public static final int NOTE_ID = 100;

public DownloadService() {
    super("DownloadService");
}

@Override
protected void onHandleIntent(Intent intent) {
    if(!intent.hasExtra(EXTRA_URL)) {
        //This Intent doesn't have anything for us
        return;
    }
    String url = intent.getStringExtra(EXTRA_URL);
    boolean result = false;
    try {
        URL url = new URL(params[0]);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        //Input stream from the connection
        InputStream in = new BufferedInputStream(connection.getInputStream());
        //Output stream to a file in your application's private space
        FileOutputStream out = openFileOutput("filename", Activity.MODE_PRIVATE);

        //Read and write the stream data here

        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    //Post a notification once complete
    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification note;
    if(result) {
        note = new Notification(0, "Download Complete", System.currentTimeMillis());
    } else {
        note = new Notification(0, "Download Failed", System.currentTimeMillis());
    }
    manager.notify(NOTE_ID, note);

}
}

Then you can call this service with the URL you want to download anywhere in an Activity like this: 然后,您可以使用要在活动中的任何位置下载的URL调用此服务,如下所示:

Intent intent = new Intent(this, DownloadService.class);
intent.putExtra(DownloadService.EXTRA_URL,"http://your.url.here");
startService(intent);

Hope that is helpful! 希望对您有所帮助!

EDIT: I'm fixing this example to remove the unnecessary double-threading for anyone who comes upon this later. 编辑:我正在修复此示例,以删除以后再出现此问题的任何人不必要的双线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM