如何通过默认的Android Downloader下载文件?

时间:2012-06-08 14:12:49

标签: android file debugging download android-2.2-froyo

如何使用Android下载程序下载文件? (WebBrowser也在使用它的下载器。)

我试过这样的事情:

Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse("MyUrl"));
startActivity(i);

有更好的方法吗?

修改

我使用的是Android 2.2

4 个答案:

答案 0 :(得分:19)

你走了。

import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class DownloadManagerActivity extends Activity {
    private long enqueue;
    private DownloadManager dm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {

                            ImageView view = (ImageView) findViewById(R.id.imageView1);
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            view.setImageURI(Uri.parse(uriString));
                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    public void onClick(View view) {
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("url for file to download"));
        enqueue = dm.enqueue(request);

    }

    public void showDownload(View view) {
        Intent i = new Intent();
        i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
        startActivity(i);
    }
}

不要忘记在清单中添加android.permission.internet

答案 1 :(得分:6)

如果您想通过网址将其下载到用户的SD卡上,只需在手机的默认互联网浏览器中打开即可。

String url = "https://appharbor.com/assets/images/stackoverflow-logo.png";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

(Code from this question)

然后将启动Android浏览器并下载文件(在本例中为图像)。

答案 2 :(得分:4)

您需要使用HttpUrlConnection在Android 2.2及更低版本上执行此操作。互联网上有一个非常详细的tutorial,告诉你如何做到这一点(也有进度对话框)。

请记住,必须使用AsyncTaskThread,以确保您在实际下载过程中不会阻止UI线程!

答案 3 :(得分:1)

是的,您无法在主线程中执行网络操作。您可以查看this存储库以下载文件。