DownloadManager-即使拥有适当的权限,下载仍失败

问题描述

在模拟器中运行Android 9(28),似乎无法让我的应用下载任何内容。我已确保分配了正确的权限,但仍然显示“下载失败”。

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

这是错误:

enter image description here

很抱歉,这很简单,我只是想学习Android。

编辑:我本来应该提到这一点,但是Android Studio在编译时不会输出错误。

编辑2:我尝试了一个小的txt文件(https://www.w3.org/TR/PNG/iso_8859-1.txt),它确实有效!我还发现尝试通过常规HTTP下载小的txt无效。但是,通过HTTPS的ZIP文件仍然无法正常工作。

通过HTTP使用5MB ZIP文件输出的Logcat:

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)

和清单权限:

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

编辑3:看起来我可以通过HTTPS下载任何大小的文件(zip或txt),但不能通过HTTP下载。不确定这是否只是DownloadManager的标准行为。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...