0

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?

Community
  • 1
  • 1
s.field212
  • 75
  • 2
  • 11

2 Answers2

0

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.

s.field212
  • 75
  • 2
  • 11
-1

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

Community
  • 1
  • 1
artkoenig
  • 7,117
  • 2
  • 40
  • 61
  • Hey, thanks for the answer. This was actually not connecting to our own server, it was just a basic get to a generic file. I suppose that does not mean your answer is wrong, and that perhaps one of the two test case sites did handle it while the other did not. Regardless, we've long since rolled our own download manager to get functionality not available to the native android one. – s.field212 Jan 14 '14 at 21:28
  • There is no reason to downvote my answer. Regarding to stackoverflow FAQ you should use downvote if "you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." – artkoenig Jun 15 '14 at 13:26