DownloadManager Not Working On Android Q - Android/Java

2

I am stuck. I have searched and searched and tried many many things let me try to summarize.

I am trying to download a file using the Download Manager on Android 10, also known as Andriod Q.

Here is the code I am using. I have tried http and https. I have added clear text flags. I have tried setDestinationInExternalFilesDir and also setDestinationInExternalPublicDir but neither work. setDestinationInExternalFilesDir does create a folder inside the Android/data directory for my application. It has no files inside the folder.

try
{
    isStoragePermissionGranted();
    Log.e(TAG, "*** STARTING  DOWNLOAD MANAGER ***");
    Uri uri = Uri.parse("http://HaveTriedManyDifferentWebServers.com/filename.jpg");
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setTitle("downloading");
    request.setDescription("Downloading a file");
    request.addRequestHeader("Accept", "image/jpg");
    request.setDestinationInExternalFilesDir(this, "", "filename.jpg");
    //request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename.png");

    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    downloadManager.enqueue(request);
    Log.e(TAG, "*** ENDING DOWNLOAD MANAGER ***");
}
catch (Exception e)
{
    Log.e("ERRRRORRRRRRRRR", e.toString());
}

I have added the permissions

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

I have checked to make sure I have write permissions. It returns "Permission is granted"

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

Why is this so difficult to do in java?

Why doesnt this return an error message?

Here is the log

2020-03-26 14:17:21.770 29975-29975/? V/jjjjjjjjjjjjjjjjjjj: Permission is granted
2020-03-26 14:17:21.770 29975-29975/? E/jjjjjjjjjjjjjjjjjjj: *** STARTING  DOWNLOAD MANAGER ***
2020-03-26 14:17:22.003 29975-29975/? E/jjjjjjjjjjjjjjjjjjj: *** ENDING DOWNLOAD MANAGER ***
2020-03-26 14:17:22.021 29975-29975/? D/ViewRootImpl@7d88[SelectionActivity]: setView = DecorView@9dfc[SelectionActivity] TM=true MM=false
2020-03-26 14:17:22.048 29975-29975/? D/ViewRootImpl@7d8[SelectionActivity]: Relayout returned: old=[0,0][1440,3040] new=[0,0][1440,3040] req=(1440,3040)0 dur=9 res=0x7 s={true 537202929664} ch=true
2020-03-26 14:17:22.049 29975-30025/? D/OpenGLRenderer: createReliableSurface : 
2020-03-26 14:17:22.049 29975-30025/? I/AdrenoGLES: QUALCOMM build                   : 
    Build Date                       : 11/25/19
    OpenGL ES Shader Compiler Version: EV031.27.05.03
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.8.1.R1.10.00.00
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2020-03-26 14:17:22.049 29975-30025/? I/AdrenoGLES: Build Config                     : S P 8.0.11 AArch64
2020-03-26 14:17:22.051 29975-29975/? E/RecyclerView: No adapter attached; skipping layout
2020-03-26 14:17:22.052 29975-30025/? I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
2020-03-26 14:17:22.068 29975-30025/? W/Gralloc3: mapper 3.x is not supported
2020-03-26 14:17:22.083 29975-29975/? D/ViewRootImpl@7d888f5[SelectionActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
2020-03-26 14:17:22.083 29975-29975/? D/InputMethodManager: prepareNavigationBarInfo() DecorView@9d0b5fc[SelectionActivity]
2020-03-26 14:17:22.083 29975-29975/? D/InputMethodManager: getNavigationBarColor() -855310
2020-03-26 14:17:22.084 29975-29975/? D/InputMethodManager: prepareNavigationBarInfo() DecorView@9d0b5fc[SelectionActivity]
2020-03-26 14:17:22.084 29975-29975/? D/InputMethodManager: getNavigationBarColor() -855310
2020-03-26 14:17:22.084 29975-29975/? V/InputMethodManager: Starting input: tba=com.example.degreemaster ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2020-03-26 14:17:22.085 29975-29975/? D/InputMethodManager: startInputInner - Id : 0
2020-03-26 14:17:22.085 29975-29975/? I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2020-03-26 14:17:22.092 29975-29975/? D/InputMethodManager: prepareNavigationBarInfo() DecorView@9d0b5fc[SelectionActivity]
2020-03-26 14:17:22.092 29975-29975/? D/InputMethodManager: getNavigationBarColor() -855310
2020-03-26 14:17:22.092 29975-29975/? V/InputMethodManager: Starting input: tba=com.example.degreemaster ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2020-03-26 14:17:22.092 29975-29975/? D/InputMethodManager: startInputInner - Id : 0
2020-03-26 14:17:22.103 29975-29975/? D/ViewRootImpl@7d888f5[SelectionActivity]: MSG_RESIZED: frame=[0,0][1440,3040] ci=[0,114][0,168] vi=[0,114][0,168] or=1

Also, I can see on the server side that the file is not even being requested. I have also used random images on google and and my own web server but neither work. From my server logs it appears that this android app is not even getting to the web server. I am able to copy/paste the URL into a browser on the phone and verify that it can successfully communicate with the web server.

EDIT:

I do have the requestLegacyExternalStorage="true" in the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:screenOrientation="portrait"
        android:requestLegacyExternalStorage="true"
        android:usesCleartextTraffic="true"
        >
        <activity android:name=".SelectionActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
java
android
asked on Stack Overflow Mar 26, 2020 by flashc5 • edited Mar 27, 2020 by flashc5

2 Answers

1

The http protocol is not allowed on Android 10 unless you request clear text traffic in manifest file.

For getExternalFilesDir() you do not need read/write permissions in manifest file nor request legacy for them. Also no code needed at run time.

answered on Stack Overflow Mar 27, 2020 by blackapps • edited Mar 27, 2020 by blackapps
1

I ran into the same problem, and finally I found out that I should use setDestinationUri() instead of setDestinationInExternalPublicDir()

And one more thing: android Q don't support Http, Use Https instead.

Below is my code:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
       request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
       request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
       request.setTitle("download");
       request.setDescription("apk downloading");
       request.setAllowedOverRoaming(false);
       request.setDestinationUri(Uri.fromFile(new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/foo/", "111.apk")));
       long downloadID = downloadManager.enqueue(request);
answered on Stack Overflow Nov 2, 2020 by williamycyh • edited Nov 2, 2020 by williamycyh

User contributions licensed under CC BY-SA 3.0