3

I've been using DownloadManager in a production app without problems but now it fails in Android Pie (API 28). Please note that the download is marked as failed and returns error 400.

@Override
public void onReceive(Context context, Intent intent) {
    long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(reference);
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor cursor = downloadManager.query(query);
    cursor.moveToFirst();
    int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
    int status = cursor.getInt(statusIndex);

    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        int fileUriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String savedFileUri = cursor.getString(fileUriIndex);

        ParcelFileDescriptor pfd = null;
        try {
            pfd = context.getContentResolver().openFileDescriptor(Uri.parse(savedFileUri), "r");
            PamplonaParkingXMLParser pamplonaParkingXMLParser = new PamplonaParkingXMLParser();
            ParkingList parkingList = pamplonaParkingXMLParser.parse(pfd);
            ObservableObject.getInstance().updateValue(parkingList);
            if (pfd != null) {
                pfd.close();
            }
        } catch (XmlPullParserException e) {
            Log.e(TAG, e.getDetail().getLocalizedMessage());
        } catch (IOException | NullPointerException e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
    } else if(status == DownloadManager.STATUS_FAILED){
        Log.e(TAG, "Download failed");
        int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
        int reason = cursor.getInt(reasonIndex);
        Log.e(TAG, reason +"");
    }
}
Javi Vazquez
  • 517
  • 6
  • 21

1 Answers1

9

Try these solutions

Solution 1)

Add this line in application tag in manifest file

android:usesCleartextTraffic="true"

like below

<application
            android:name=".ApplicationClass"
            android:usesCleartextTraffic="true"
            android:networkSecurityConfig="@xml/network_security_config"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

Add this tag in application tag in manifest file

Solution 2)

Add android:networkSecurityConfig="@xml/network_security_config" in application tag

<application
        android:name=".ApplicationClass"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

where network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

enter image description here

Create xml under res directory and then network_security_config.xml in xml folder For more info refer my answer Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • 1
    Thank you, I was using and so on trying to explicitly allow only clear text traffic to a domain, but didn't work. Your solution finally solved the issue. – Javi Vazquez Dec 31 '18 at 19:01
  • i am glag it worked , please accept my answer to others so others can use it – Quick learner Jan 02 '19 at 05:35
  • Setting cleartextTraffic permission for all domains by default is not very secure way how to solve this. I wander, if there is a possibility to discover what exactly causes error 400. In my case, I know the particular URL and domain, however simply don't work :( – Peter Knut Sep 26 '19 at 07:24
  • try the solution 2 and let me know – Quick learner Sep 26 '19 at 07:29