Откройте загруженный файл с помощью диспетчера загрузки Android

1

Я использовал этот код для загрузки pdf файла в своем приложении, и он хорошо работает для всех телефонов, но код, который я написал для открытия загруженного файла, хорошо работает для APK менее 21, но не для лучших телефонов, таких как Samsung 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();

}
Теги:
android-studio

2 ответа

0

У вас есть исключение? Попробуйте использовать FileProvier для API> 23.

Откройте загруженный файл на Android N с помощью FileProvider

  • 0
    всякий раз, когда я пытаюсь открыть любую загруженную книгу, приложение вылетает
  • 0
    Потому что на API> 23 вы должны использовать FileProvider
0

Попробуй это

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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню