0

Running Android 9 (28) in an emulator, and can't seem to get my application to download anything. I've made sure the correct permissions are assigned, but it still says "Download unsuccessful."

package com.example.download_test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("dm","We in bois");
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        getZipFile();
        Log.d("dm","it should have worked yikes");
    }

    private int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
    private String extension;

    public void getZipFile() {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE);
            Log.e("dm","Permissions no worky");
        } else {
            //TODO
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                Log.v("dm","Permission is granted");
                //File write logic here
                extension =".zip";
                Uri uri = Uri.parse("http://ipv4.download.thinkbroadband.com/5MB.zip");
                Log.d("dm","URI Parsed");
                DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setTitle("5MB.zip");
                request.allowScanningByMediaScanner();
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "5MB" + extension);
                request.setMimeType("application/zip");
                request.setNotificationVisibility(1);
                downloadmanager.enqueue(request);
                Log.i("dm", "Download requested");
            }
        }
    }
}

Here's the error:

enter image description here

My apologies if this is trivial, I'm just trying to learn about Android.

Edit: I should have mentioned this originally, but Android Studio doesn't output an error when compiling.

Edit 2: I tried with a small txt file (https://www.w3.org/TR/PNG/iso_8859-1.txt) and it worked! I also found that trying to download a small txt over regular HTTP does not work. A ZIP file over HTTPS still doesn't work either, though.

Logcat output with 5MB ZIP file over HTTP:

2020-10-12 10:26:01.427 3955-3955/? I/e.download_tes: Not late-enabling -Xcheck:jni (already on)
2020-10-12 10:26:01.444 3955-3955/? W/e.download_tes: Unexpected CPU variant for X86 using defaults: x86_64
2020-10-12 10:26:01.752 3955-3955/com.example.download_test D/dm: We in bois
2020-10-12 10:26:01.762 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-10-12 10:26:01.762 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-10-12 10:26:01.762 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-10-12 10:26:01.762 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-10-12 10:26:01.762 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-10-12 10:26:01.773 3955-3955/com.example.download_test V/dm: Permission is granted
2020-10-12 10:26:01.773 3955-3955/com.example.download_test D/dm: URI Parsed
2020-10-12 10:26:01.807 3955-3955/com.example.download_test I/dm: Download requested
2020-10-12 10:26:01.807 3955-3955/com.example.download_test D/dm: it should have worked yikes
2020-10-12 10:26:01.931 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-10-12 10:26:01.932 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-10-12 10:26:01.946 3955-3955/com.example.download_test W/e.download_tes: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-10-12 10:26:01.970 3955-3955/com.example.download_test D/OpenGLRenderer: HWUI GL Pipeline
2020-10-12 10:26:02.025 3955-3984/com.example.download_test I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-10-12 10:26:02.025 3955-3984/com.example.download_test I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-10-12 10:26:02.025 3955-3984/com.example.download_test I/OpenGLRenderer: Initialized EGL, version 1.4
2020-10-12 10:26:02.025 3955-3984/com.example.download_test D/OpenGLRenderer: Swap behavior 1
2020-10-12 10:26:02.026 3955-3984/com.example.download_test W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-10-12 10:26:02.026 3955-3984/com.example.download_test D/OpenGLRenderer: Swap behavior 0
2020-10-12 10:26:02.061 3955-3984/com.example.download_test D/EGL_emulation: eglCreateContext: 0x715ffe6444a0: maj 2 min 0 rcv 2
2020-10-12 10:26:02.088 3955-3984/com.example.download_test D/EGL_emulation: eglMakeCurrent: 0x715ffe6444a0: ver 2 0 (tinfo 0x715ffe60dc20)
2020-10-12 10:26:02.168 3955-3984/com.example.download_test D/EGL_emulation: eglMakeCurrent: 0x715ffe6444a0: ver 2 0 (tinfo 0x715ffe60dc20)
2020-10-12 10:26:17.002 3955-3984/com.example.download_test D/EGL_emulation: eglMakeCurrent: 0x715ffe6444a0: ver 2 0 (tinfo 0x715ffe60dc20)

and Manifest Permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Edit 3: Looks like I can download any size file (zip or txt) over HTTPS, but not over HTTP. Not sure if this is just standard behavior for DownloadManager.

0 Answers0