簡體   English   中英

打開使用Android下載管理器下載的文件

[英]Open a file downloaded using download manager Android

我已使用此代碼在我的應用程序中下載pdf文件,並且適用於所有手機,但是我編寫的打開下載文件的代碼適用於APK小於21的APK,但不適用於三星S7等更好的手機。

我下載pdf文件的代碼:

Boolean memoerAvailable = (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
if (memoerAvailable) {
  final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
  request.setTitle("Click to download the book");
  request.setDescription("File is being downloaded...");

  //use download manager to download the clicked book
  request.allowScanningByMediaScanner();
  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, ShowbooksActivity.myBooksList.get(ShowbooksActivity.pos).getMpdfName());
  request.setDestinationInExternalFilesDir(bookWithAdapterActivity.this, Environment.DIRECTORY_DOWNLOADS, "myfile.jpg");
  final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  final long queueid = manager.enqueue(request);

我打開下載文件的代碼:

filetoopen = ls1.get(position);
File file = new File(Environment.getExternalStorageDirectory() +"/Download/" + filetoopen);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
  startActivity(intent);
} catch (ActivityNotFoundException e) {
  Toast.makeText(getContext(), "File not found", Toast.LENGTH_SHORT).show();

}

你有一個exeption? 嘗試對File> 23使用FileProvier。

使用FileProvider在Android N上打開下載的文件

試試這個

setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimeType,
                                    long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                    mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                            url, contentDisposition, mimeType));
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File",
                    Toast.LENGTH_LONG).show();
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM