• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Android download manager not working for me

Hi everyone i am a bit stuck with DownloadManager trying to add a download functionality to my sample app. the problem is that when i execute the code to download a file nothing happen and after about 15 min a notification show up ( unsuccessful download ).

here is the code i used :

downloadManager  = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("Sample video name.mp4");
Long reference = downloadManager.enqueue(request);

Any help will be appreciated.

Use this function to download the file using the Download manager:

private long downloadFile(Uri uri, String fileStorageDestinationUri, String fileName) {

    long downloadReference = 0;

    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
    try {
        DownloadManager.Request request = new DownloadManager.Request(uri);

        //Setting title of request
        request.setTitle(fileName);

        //Setting description of request
        request.setDescription("Your file is downloading");

        //set notification when download completed
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        //Set the local destination for the downloaded file to a path within the application's external files directory
        request.setDestinationInExternalPublicDir(fileStorageDestinationUri, fileName);

        request.allowScanningByMediaScanner();

        //Enqueue download and save the referenceId
        downloadReference = downloadManager.enqueue(request);
    } catch (IllegalArgumentException e) {
        BaseUtils.showToast(mContext, "Download link is broken or not availale for download");
        Log.e(TAG, "Line no: 455,Method: downloadFile: Download link is broken");

    }
    return downloadReference;
}

Now call this method like below:

long downloadFileRef = downloadFile(Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"), "/YourAppName/", "fileName.mp4");
if (downloadFileRef != 0) {
    BaseUtils.showToast(mContext, "Starting download...");
}else {
    BaseUtils.showToast(mContext, "File is not available for download");
}

You can find the downloaded file in Storage/YourAppName/fileName.mp4 in your device

  • 1
Reply Report
      • 1
    • looks like it's about android version the emulator that the code works on is android 7 and my device which the code does not work on is android 8.1 to confirm that i installed new emu with android 8.1 and yeah it's not working

Here is how i implement it:

public void downloadHD(){
    long downloadFileRef = downloadFile(Uri.parse(Video.VideoUrl()), "/"+getString(R.string.app_name)+"/", Video.VideoName()+" HD"+".mp4");
    if (downloadFileRef != 0) {
        this.showToast(getString(R.string.startdownload));
    }else {
        this.showToast(getString(R.string.notavialblefordownload));
    }
}

then :

private long downloadFile(Uri uri, String fileStorageDestinationUri, String fileName) {

        long downloadReference = 0;

        downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        try {
            DownloadManager.Request request = new DownloadManager.Request(uri);

            //Setting title of request
            request.setTitle(fileName);

            //Setting description of request
            request.setDescription(getString(R.string.downloadingnow));

            //set notification when download completed
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

            //Set the local destination for the downloaded file to a path within the application's external files directory
            request.setDestinationInExternalPublicDir(fileStorageDestinationUri, fileName);

            request.allowScanningByMediaScanner();

            //Enqueue download and save the referenceId
            downloadReference = downloadManager.enqueue(request);
        } catch (IllegalArgumentException e) {
           this.showToast(getString(R.string.downloadnotavilablenow));


        }
        return downloadReference;
    }
  • 0
Reply Report