gpt4 book ai didi

android - 下载管理器不适用于 LG 设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:45 25 4
gpt4 key购买 nike

我尝试使用 DownloadManager 执行下载文件.除 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);
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();
}

在 LG 设备线上

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

返回-1。下载通知出现在通知栏中,并在几毫秒后立即消失。无一异常(exception),没有人进入这段代码

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;
}

什么都没有。所以内部 while() 循环永远工作。在 LG D802 和 LG G3 S 设备上测试。这两种设备表现出相同的行为。

最佳答案

我遇到了同样的问题,并找到了解决方案。在初始化下载管理器之前,您需要确保所有目录都存在。示例:在我的例子中,我的文件路径是/storage/emulated/0/MyFolder/n.jpeg所以我必须确保 MyFolder 目录在初始化下载管理器之前存在。

关于android - 下载管理器不适用于 LG 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41679016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com