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

发布于 2024-10-15 07:47:22 字数 311 浏览 5 评论 0原文

我正在开发一个应用程序,我需要下载一个大小为 5 到 50 MB 的文件(.zip / .txt / .jpg 等)。基于 Android 2.2 的应用程序。

用户提供URL并触发下载,但下载过程会在后台运行直至完成。

流媒体应用于下载文件。
我想知道如何使用 HTTP 连接来完成此操作。
哪些可以用于此目的?
android 2.2是否为此提供了API?

任何形式的帮助表示赞赏......

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.

The user provides the URL and triggers the download but then the downloading process runs in background until complete.

streaming should be used for downloading file.
I want to know how can this be done using HTTP connections.
what classes can be used for this?
Does android 2.2 provides an API for this?

Any kind of help is appreciated....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情何以堪。 2024-10-22 07:47:22

Android 确实为此目的包含了一个名为 DownloadManager 的 API...但它是在 2.3 中发布的;因此,虽然它在面向 2.2 的应用程序中没有用处,但它仍然可能是您研究实现的良好资源。

我推荐的一个简单实现是这样的:

  • 使用 HttpURLConnection 来连接和下载数据。这将需要在清单中声明 INTERNET 权限,
  • 确定文件所在的位置。如果您希望将其存储在设备的 SD 卡上,则还需要 WRITE_EXTERNAL_STORAGE 权限。
  • 将此操作包装在 AsyncTask 的 doInBackground() 方法中。这是一个长时间运行的操作,因此您需要将其放入 AsyncTask 为您管理的后台线程中。
  • Service 中实现此操作,以便操作可以在受保护的情况下运行,而无需用户将 Activity 保留在前台。
  • 下载完成后,使用 NotificationManager 通知用户,这会向用户的状态栏发布一条消息。

为了进一步简化事情,如果您使用 IntentService,它将为您处理线程(onHandleIntent 中的所有内容都在后台线程上调用),并且您可以对多个下载进行排队只需向其发送多个 Intent 即可一次处理一个。这是我所说的一个框架示例:

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);

}
}

然后您可以使用要在 Activity 中的任何位置下载的 URL 来调用此服务,如下所示:

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

希望这有帮助!

编辑:我正在修复此示例,以便为以后遇到此问题的任何人删除不必要的双线程。

Android did include an API called DownloadManager for just this purpose...but it was release in 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.

A simple implementation I would recommend is something like this:

  • Use an HttpURLConnection to connect and download the data. This will require the INTERNET permission to be declared in your manifest
  • 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.
  • Wrap this operation in the doInBackground() method of an AsyncTask. This is a long-running operation, so you need to put it into a background thread, which AsyncTask manages for you.
  • Implement this in a Service so the operation can run protected without the user keeping the an Activity in the foreground.
  • Use NotificationManager to notify the user when the download is complete, which will post a message to their status bar.

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. 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文