Download Speed is slow in Android

Viewed 50

I have a list of PDFs that I need to download all PDFs in one click. I have implemented a foreground service to do the task. In this service I am enqueuing the list of PDFs in the Android DownloadManager's request to handle the downloads.

For the enqueue of request, I am inserting 6 PDF's in request for the first time and further when any one download completed I am inserting next PDF in the request list.

In this implementation the whole process is taking around 1-2 mins for the 91 PDFs(roughly size of 40MB in total). Is there any way, so we can speed up the download process.

This is the function to start Download queue

private void startDownloadQueue() 
{
    for(int i=0;i<6;i++){
        if(i<hashMap.size()) {
            listIndex++;
            initiateDownload(documentList.get(i), positionList.get(i), hashMap.size());
        }
    }
}


private void initiateDownload(DocumentJsonModel.Document doc, int pos, int listSize)
{
    if (doc != null && isDownloading) {
        String position = String.valueOf(pos);
        String docId = doc.id;
        String docFile = doc.file;
        String docName = doc.name;
        String docDescription = doc.description;
        boolean isJustView = false;
        isFromSearch = false;

        long id = downloadFile(doc, docId, docFile, docName, docDescription, position, isJustView, listSize);
        if (id != 0) {
            setProgressUpdate(id, Integer.parseInt(position), Integer.parseInt(docId), isJustView, doc, listSize);
        } else if (isJustView) {
            Intent intent1 = new Intent(INTENT_FILTER_ACTION_OPEN_DOC);
            intent1.putExtra("docID",docId);
            intent1.putExtra("position",pos);
            intent1.putExtra("fromDownloadAll", true);
            intent1.putExtra("docId", docId);
            sendBroadcast(intent1);
            stopForeground(true);
            stopSelf();
        } else if (isFromSearch) {
            Intent intent1 = new Intent(INTENT_FILTER_ACTION_OPEN_DOC_FROM_SEARCH);
            intent1.putExtra("fromDownloadAll", true);
            intent1.putExtra("position", pos);
            intent1.putExtra("docId", docId);
            sendBroadcast(intent1);
            stopForeground(true);
            stopSelf();
        }
    }
}

Method to enqueue PDFs in download manager request queue -

public long downloadFile(DocumentJsonModel.Document doc,String docId, String docFile, String docName, String description, String position, boolean isJustView, int listSize) {
    long downloadReference = 0;
    String root = "/twoway/saved_documents/";
    String BASE_DIR = getExternalFilesDir(null).getPath();

    File dir = new File(BASE_DIR + root);
    File file = new File(dir, docId + "_" + docFile);
    if(!isCancelledButtonPressed) {
        if (!file.exists()) {
            String fileUrl = DOCUMENT_URL + docFile;
            fileUrl = fileUrl.replaceAll(" ", "%20");

            Uri fileUri = Uri.parse(fileUrl);
            //Create request for android download manager
            DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(fileUri);
            //set title for download
            request.setTitle(docName);
            request.setVisibleInDownloadsUi(false);
            //request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
            //Setting description of request
            request.setDescription(description);

            request.setDestinationInExternalFilesDir(this, null, "/twoway/saved_documents/"
                    + docId + "_" + docFile);

            //Enqueue download and save into referenceId
            downloadReference = downloadManager.enqueue(request);



        }

}}

0 Answers
Related