2017-01-16 14 views
7

Tôi cố gắng tải xuống tệp bằng cách sử dụng DownloadManager. Mọi thứ hoạt động tốt trừ các thiết bị LG. Đây là mã của tôi:Trình quản lý tải xuống không hoạt động trên các thiết bị 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(); 
} 

Trên các thiết bị LG dòng

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

lợi nhuận -1. Thông báo tải xuống xuất hiện trong thanh thông báo và biến mất ngay lập tức sau vài mili giây. Không có ngoại lệ, không ai nhập mã này

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

Chỉ cần không có gì. Vì vậy, nội bộ trong khi() chu kỳ làm việc mãi mãi. Thử nghiệm trên các thiết bị LG D802 và LG G3 S. Cả hai thiết bị đều hiển thị cùng một hành vi.

+0

có hoạt động không nếu bạn thử sử dụng URL không sử dụng HTTPS? Gần đây tôi đã chạy vào cùng một vấn đề và cho đến nay có vẻ như nó hoạt động nếu tôi chuyển từ https sang http. Đó không phải lúc nào cũng là một lựa chọn, nhưng nếu có thể nó có thể làm việc cho bạn. –

Trả lời

0

Tôi gặp vấn đề tương tự và tìm ra giải pháp cho việc này. Bạn cần phải chắc chắn rằng tất cả các thư mục tồn tại trước khi init Download Manager. Ví dụ: trong trường hợp của tôi đường dẫn đến tập tin của tôi là /storage/emulated/0/MyFolder/n.jpeg vì vậy tôi phải chắc chắn Myfolder thư mục tồn tại trước khi init Download Manager.

Các vấn đề liên quan