7
DownloadManager을 사용하여 파일을 다운로드하려고합니다. LG 장치를 제외하고 모두 제대로 작동합니다. LG 장치 라인LG 기기에서 다운로드 관리자가 작동하지 않습니다.
bytes_total = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
-1을 반환에
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();
}
: 여기 내 코드입니다. 다운로드 알림이 알림 표시 줄에 나타나고 몇 밀리 초가 지나면 즉시 사라집니다. 아무도 예외는 아니며이 코드 섹션을 입력 할 사람이 없습니다
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에서 테스트되었습니다. 두 장치 모두 동일한 동작을 나타냅니다.
HTTPS를 사용하지 않는 URL을 사용해도 작동합니까? 최근에 같은 문제가 발생하여 https에서 http로 전환하면 작동하는 것처럼 보입니다. 항상 옵션은 아니지만, 가능하다면 당신을 위해 일할 수 있습니다. –