14

How can I download files using Android downloader? (The downloader that WebBrowser is using that too).

I tried something like this :

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

Any better way?

Edit

I am using Android 2.2

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Kermia
  • 4,171
  • 13
  • 64
  • 105

4 Answers4

20

Here you go.

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

Don't forget to add android.permission.internet in manifest.

Vipul
  • 27,808
  • 7
  • 60
  • 75
6

If you want to download it onto the user's SD card from an URL, you can just open it in the phone's default internet browser.

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)

The android browser will then start and the file (in this case an image) will be downloaded.

Community
  • 1
  • 1
Todd Davies
  • 5,484
  • 8
  • 47
  • 71
  • In My sony xepria phone the.The image is shown in the browser it self.What I want is after firing intent when download starts it should move to my app. – Ashwin N Bhanushali Dec 11 '12 at 06:31
  • If you're trying to download an image then the browser will open the image and view it. This method is meant for binary files such as .zip. I recommend you use Vipul Shah's solution. – Todd Davies Dec 11 '12 at 17:01
4

You need to use HttpUrlConnection to do this on Android 2.2 and below. There is a very detailed tutorial on the internet that shows you how to do this (with a progress dialog box too).

Remember you must use an AsyncTask or a Thread to ensure that you do not block the UI thread during the actual download!

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Thank you, But "How can I download files using default Android downloader?". I think I should use my own solution. – Kermia Jun 14 '12 at 07:03
  • 1
    If you are working on a device that runs Android 2.2 or below, you can't. The default downloader was introduced in 2.3 and thus earlier versions won't recognize it. – Alex Lockwood Jun 14 '12 at 12:28
1

Yes, you cant perform network operation in the main thread. You can look up to this repository to download file.

Amit Pathak
  • 186
  • 3