2017-09-28 10 views
0

웹 서버에서 파일을 다운로드하려면 DownloadManager을 사용하고 있습니다.다운로드 관리자가 다운로드 한 파일 열기

처음으로 DownloadManager.Request을 만들고 헤더를 추가하고 제목, 설명, 알림, MimeType에 데이터를 추가 한 다음 큐에 넣습니다.

파일 다운로드가 완료 될 때까지 기다린 후 uri을 얻은 다음 파일을 열 의도를 만듭니다.

선택한 프로그램을 통해 파일 (PDF 또는 txt)을 열려면 Google Pdf Viewer, HTML 뷰어, Chrome 및 다른 프로그램을 사용해 보았는데 파일을 열 수 없다고 항상 말합니다. 상단 바를 통해 열기를 원할 때 DownloadManager 알림을 받으면 파일이 올바르게 열립니다. 기본 다운로드 대상이 공간을 확보 할 필요가있는 경우 시스템이 파일을 삭제할 수있는 공유 볼륨이기 때문에

public void getFileContent(Map<String, String> headers) { 
    if (downloadManager != null) { 
     DownloadManager.Request request = getRequest(headers); 
     Long fileId = downloadManager.enqueue(request); 
     compositeSubscription.add(RxDownloader.getInstance(getActivity()) 
       .download(request) 
       .subscribe(path -> showFileContent(Uri.parse(path)), 
         throwable -> showError(throwable.getMessage()))); 
    } 
} 

private DownloadManager.Request getRequest(Map<String, String> headers) { 
    Uri uri = Uri.parse(BuildConfig.API_URL + "api/v2/files/" + fileResource.getId() + "/raw"); 
    DownloadManager.Request request = new DownloadManager.Request(uri); 
    request = addRequestHeaders(request, headers); 
    request = setRequestData(request); 
    return request; 
} 

private DownloadManager.Request addRequestHeaders(DownloadManager.Request request, Map<String, String> headers) { 
    for (Map.Entry<String, String> entry : headers.entrySet()) { 
     request.addRequestHeader(entry.getKey(), entry.getValue()); 
    } 
    return request; 
} 

private DownloadManager.Request setRequestData(DownloadManager.Request request) { 
    request.setTitle(getString(R.string.file_downloader)); 
    request.setDescription(String.format(getString(R.string.fmt_downloading), fileResource.getFileName())); 
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
    request.setMimeType(fileResource.getMimeType()); 
    return request; 
} 

private void showFileContent(Uri uri) { 
    Intent target = new Intent(Intent.ACTION_VIEW); 
    target.setDataAndType(uri, fileResource.getMimeType()); 
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 

    Intent intent = Intent.createChooser(target, getString(R.string.open_file)); 
    try { 
     startActivity(intent); 
    } catch (ActivityNotFoundException e) { 
     showError(getString(R.string.no_application_installed)); 
    } 
} 

답변