1

I am always getting COLUMN_STATUS as STATUS_SUCCESSFUL even it fails due to insufficient space:

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent == null) {
            Utils.log(TAG, "Intent is NULL");
            return;
        }

        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
            DownloadManager downloadManager = (DownloadManager)
                    context.getSystemService(Context.DOWNLOAD_SERVICE);
            long referenceId = intent.getLongExtra(
                    DownloadManager.EXTRA_DOWNLOAD_ID, -1L);

            if (downloadManager != null) {
                DownloadManager.Query dmQuery = new DownloadManager.Query();
                dmQuery.setFilterById(referenceId);
                try (Cursor cursor = downloadManager.query(dmQuery)) {
                    if (cursor != null && cursor.getCount() > 0) {
                        // The error is here
                        int columnIndex =cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (cursor.moveToFirst() && cursor.getInt(columnIndex) == DownloadManager.STATUS_SUCCESSFUL) {

                        } else {
                            Log.e(TAG, "fail");
                        }
                    }

            } catch (Exception exception) {
                Log.e(TAG, exception);
            }                
            }
        }
    }
pckill
  • 3,709
  • 36
  • 48
AshokG
  • 21
  • 4

1 Answers1

1

If your connection timeouts, you will get FAILED. If it fails due to insufficient space, check the query columns COLUMN_TOTAL_SIZE_BYTES and COLUMN_BYTES_DOWNLOADED_SO_FAR to check if equal.

int bytes_total = cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
int bytes_downloaded = cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));