Question

I'm trying to get download manager request working and I'm getting a strange error. Here's the code:

package com.vsnetworks.vswebkads;

import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.util.Log;

@TargetApi(9)
public class MainActivity extends Activity {

static final String DL_STR = "downloads";

@Override                                                                            
public void onCreate(Bundle savedInstanceState) {

    Log.v("VERS TEST", "1.1");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File externalDir = getExternalFilesDir(null);
    externalDir.mkdirs();

    File dlDir = new File(externalDir, MainActivity.DL_STR);
    if (!dlDir.exists())
        dlDir.mkdirs();

    DownloadManager dlManager = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse("https://www.google.com/images/srpr/logo3w.png");
    //uri = Uri.parse("http://download.thinkbroadband.com/20MB.zip");
    DownloadManager.Request request = new Request(uri);
    request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
    request.setDestinationInExternalFilesDir(getApplicationContext(), null, "downloads/test.png");
    request.setTitle("test");
    dlManager.enqueue(request);
}
}

Interestingly, if I uncomment the second uri = line, the download works great and the file ends up in the directory. Which leads me to think it's that I'm trying to download via SSL that is causing the problem. However, according to these two threads ssl is supported for download manager (in comments for the first one):

Android DownloadManager and SSL (https)

android2.3 DownloadManager

I also don't seem to get any error telling me download manager only works with http like in the second link. Here's the error I do see:

01-22 17:23:42.385: I/DownloadManager(1061): Initiating request for download 4068
01-22 17:23:42.905: I/DownloadManager(1061): Initiating request for download 4068
01-22 17:23:42.905: W/DownloadManager(1061): Aborting request for download 4068: Trying to resume a download that can't be resumed
01-22 17:23:42.905: D/DownloadManager(1061): setupDestinationFile() unable to resume download, deleting /storage/sdcard0/Android/data/com.vsnetworks.vswebkads/files/downloads/test.png
01-22 17:23:42.915: D/DownloadManager(1061): cleanupDestination() deleting /storage/sdcard0/Android/data/com.vsnetworks.vswebkads/files/downloads/test.png

I just updated android on my tablet today, it's sitting on version 4.1.1, so I don't think it's a version problem.

So, can I do a download manager request using https, and if so, is this the correct method to do it?

Was it helpful?

Solution

In this case, the error appears to be device related. When we switched testing to a nexus 10, the code worked and this error did not appear.

OTHER TIPS

Check your server logs, sometimes the DownloadManager initiates a partial download request (which cause the misleading double log output "Initiating request for download". Your server has to handle this request properly, otherwise you get this error. See my answer for this question: Can't resume download

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top