下载管理器不能在LG设备上工作 [英] Download manager does not work on LG devices

查看:440
本文介绍了下载管理器不能在LG设备上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用下载管理器执行下载文件。一切工作正常,除了LG设备。这是我的代码:

  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);
文件dir = new File(FileUtil.getInstance()。getDownloadedFilesDir());
if(!dir.exists()){
dir.mkdirs(); (new File(FileUtil.getInstance()。)getDownloadedFilesDir()+ File.separator + fileInfo.getmName())));}
}
request.setDestinationUri(Uri.fromFile

downloadId = manager.enqueue(request); $(b)









$ Download $ 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));
(); $ b $ catch(CursorIndexOutOfBoundsException e){
e.printStackTrace();
cursor.close(); $ b $(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); b break;
} catch(IllegalArgumentException e){
e.printStackTrace();
cursor.close();
break; (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))== DownloadManager.STATUS_FAILED){
下载= false;

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

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

})。start();

$ / code>

在LG设备上

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

返回-1。
下载通知显示在通知栏中,几毫秒后立即消失。没有人例外,没有人进入这段代码。

  if(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))= = DownloadManager.STATUS_FAILED){
下载= false;
cursor.close();
onDownloadFail();
break; (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))== DownloadManager.STATUS_SUCCESSFUL){
downloaded = false;

else if(cursor.getInt
cursor.close();
break;
}

没什么。所以内部while()循环永远工作。
在设备LG D802和LG G3 S上测试。两个设备显示相同的行为。

问题,并找到了解决办法。你需要确保所有的目录都存在于init下载管理器之前。
示例:在我的情况下,我的文件的路径是
/storage/emulated/0/MyFolder/n.jpeg
,所以我必须确保 MyFolder 目录存在于init下载管理器之前。


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.

解决方案

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.

这篇关于下载管理器不能在LG设备上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆