Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Android DownloadManager?

I want to download a file to SDCard with Android DownloadManager class:

Request request = new Request(Uri.parse(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); //set destination dir
long downloadId = downloader.enqueue(request);

But I always get download status=16(STATUS_FAILED), and reason=1008(ERROR_CANNOT_RESUME). I have already included android.permission.WRITE_EXTERNAL_STORAGE in the manifest.

When i commented out the

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); 

and use the default download folder, it's OK. But I don't know where is the file goes, the localUri I get from the result is something like:

content://downloads/my_downloads/95

I don't know how to copy the file to SDCard.

What I want is download a file to SDCard. Could someone help? Thanks!

like image 203
Dagang Avatar asked Feb 08 '12 13:02

Dagang


People also ask

How do I use download manager on Android?

Once you are sure DownloadManager is available, you can do something like this: String url = "url you want to download"; DownloadManager. Request request = new DownloadManager. Request(Uri.

Where does download manager save files Android?

You can find downloads on Android in My Files or File Manager. You can find these apps in the app drawer of your Android device. Within My Files or File Manager, navigate to the Downloads folder to find everything you downloaded.


2 Answers

You can retrieve file path from localUri like this:

public static String getFilePathFromUri(Context c, Uri uri) {
    String filePath = null;
    if ("content".equals(uri.getScheme())) {
        String[] filePathColumn = { MediaColumns.DATA };
        ContentResolver contentResolver = c.getContentResolver();

        Cursor cursor = contentResolver.query(uri, filePathColumn, null,
                null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
    } else if ("file".equals(uri.getScheme())) {
        filePath = new File(uri.getPath()).getAbsolutePath();
    }
    return filePath;
}
like image 164
Min Avatar answered Nov 15 '22 16:11

Min


Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() gives me /mnt/sdcard/downloads

And I'm able to use the downloaded file in onReceive (ACTION_DOWNLOAD_COMPLETE)

long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(downloadId);
Cursor cur = dm.query(query);

if (cur.moveToFirst()) {
    int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
    if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) {
        String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

        File mFile = new File(Uri.parse(uriString).getPath());
        ....

    } else {
        Toast.makeText(c, R.string.fail, Toast.LENGTH_SHORT).show();
    }
}
like image 33
Mikhail Avatar answered Nov 15 '22 16:11

Mikhail