【问题标题】:How do I download a file via default Android Downloader?如何通过默认的 Android 下载器下载文件?
【发布时间】:2012-06-12 14:41:46
【问题描述】:

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

我试过这样的:

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

有更好的方法吗?

编辑

我使用的是 Android 2.2

【问题讨论】:

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


    【解决方案1】:

    给你。

    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

    【讨论】:

    • 很好的答案。其实我不知道有这样的服务存在。是粘性广播吗?
    • 抱歉不确定:)。如果它解决了你的问题,你可以接受这个答案,因为它会帮助别人:)
    • @VipulShah 谢谢,但是 Android 2.3 中引入了 DownloadManager,我使用的是 Android 2.2。那么解决办法是什么?
    • @Kermia:在 Android 2.2 及更低版本上,您可以使用HttpUrlConnectionHttpClient 自行下载文件。
    • 我们可以使用 httpurlconnection 下载 youtube 视频
    【解决方案2】:

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

    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 浏览器将启动并下载文件(在本例中为图像)。

    【讨论】:

    • 在我的索尼 xepria 手机中。图像显示在浏览器中。我想要的是在下载开始时触发意图后它应该移动到我的应用程序。
    • 如果您尝试下载图像,则浏览器将打开图像并查看它。此方法适用于二进制文件,例如 .zip。我建议您使用 Vipul Shah 的解决方案。
    【解决方案3】:

    您需要使用HttpUrlConnection 在 Android 2.2 及更低版本上执行此操作。互联网上有一个非常详细的tutorial 向您展示了如何执行此操作(也带有进度对话框)。

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

    【讨论】:

    • 谢谢,但是“如何使用默认的 Android 下载器下载文件?”。我想我应该使用自己的解决方案。
    • 如果您使用的是运行 Android 2.2 或更低版本的设备,则不能。默认下载器是在 2.3 中引入的,因此早期版本无法识别它。
    【解决方案4】:

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

    【讨论】:

    • 嗨阿米特,也许你可以看看here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2015-10-23
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多