Download manager does not work on LG devices

[亡魂溺海] 提交于 2019-12-12 10:38:27

问题


I try to perform downloading the file using DownloadManager. Everything works fine except LG devices. Here is my code:

private void downloadFile(FileInfo fileInfo) {
    private DownloadManager manager = (DownloadManager) MyApp.getSingleton().
            getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://"
            + MyPreference.getInstance().getBaseUrl()
            + "/api/v3/files/"
            + fileId
            + "/get"));

    request.setDescription(MyApp.getSingleton().getApplicationContext().getString(R.string.downloading));
    request.setTitle(fileInfo.getmName());
    request.addRequestHeader("Authorization", "Bearer " + MyPreference.getInstance().getAuthToken());
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    File dir = new File(FileUtil.getInstance().getDownloadedFilesDir());
    if(!dir.exists()){
        dir.mkdirs();
    }
    request.setDestinationUri(Uri.fromFile(new File(FileUtil.getInstance().getDownloadedFilesDir() + File.separator + fileInfo.getmName())));

    downloadId = manager.enqueue(request);

    new Thread(() -> {

        boolean downloading = true;
        while (downloading) {

            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(downloadId);

            Cursor cursor = manager.query(q);
            cursor.moveToFirst();
            int bytes_downloaded = 0;
            long bytes_total = 0;
            try {
                bytes_downloaded = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                bytes_total = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
            } catch (CursorIndexOutOfBoundsException e) {
                e.printStackTrace();
                cursor.close();
                break;
            } catch (IllegalArgumentException e){
                e.printStackTrace();
                cursor.close();
                break;
            }
            if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
                downloading = false;
                cursor.close();
                onDownloadFail();
                break;
            } else if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                downloading = false;
                cursor.close();
                break;
            }

            if (bytes_total > 0) {
                final int dl_progress = (int) ((bytes_downloaded * 100L) / bytes_total);
                    onProgress(dl_progress);
            }
            cursor.close();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }).start();
}

On LG devices line

bytes_total = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

returns -1. Download notification appears in notification bar and disappears immediately after few milliseconds. No one exception, no one enter this code section

if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
                downloading = false;
                cursor.close();
                onDownloadFail();
                break;
            }
else if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                downloading = false;
                cursor.close();
                break;
            }

Just nothing. So internal while() cycle work forever. Tested on devices LG D802 and LG G3 S. Both devices show the same behavior.


回答1:


I faced the same issue and found the solution for this. You need to make sure all the directories exists before init Download Manager. Example: in my case the path to my File is /storage/emulated/0/MyFolder/n.jpeg so I have to make sure MyFolder directory exists before init Download Manager.



来源:https://stackoverflow.com/questions/41679016/download-manager-does-not-work-on-lg-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!