В веб-просмотре загрузка файла работает только на моем телефоне Xiaomi Redmi 5 и не работает на других телефонах. - PullRequest
0 голосов
/ 02 августа 2020

`yondaftar.setDownloadListener (new DownloadListener () {@RequiresApi (api = Build.VERSION_CODES.JELLY_BEAN_MR1) @Override publi c void onDownloadStart (String url, String userAgent, String contentDisposition) {long if contentDisposition, String if contentDisposition (Build.VERSION.SDK_INT> = Build.VERSION_CODES.M) {if (checkSelfPermission (Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {DownloadDialog (url, userAgent, contentDispater); MainActivity.this, new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);}} else {DownloadDialog (url, userAgent, contentDisposition, mimeType);}}});

@ SuppressLint ("SetTextI18n ") @RequiresApi (api = Build.VERSION_CODES.JELLY_BEAN_MR1) publi c void DownloadDialog (final String url, final String userAgent, final String contentDisposition, final String mimeType) {final String filename = URLUtil.guessFileName (url, mime contentDisposition );

    final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
            MainActivity.this,R.style.BottomSheetDialoTheme
    );
    bottomSheetDialog.setCancelable(false);
    final View bottomSheetView = LayoutInflater.from(getApplicationContext())
            .inflate(
                    R.layout.download_sheets,
                    (ConstraintLayout)findViewById(R.id.bottomSheetContainer)
            );
    bottomSheetView.findViewById(R.id.close_txt).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bottomSheetDialog.dismiss();
        }
    });
    final TextView file_name = bottomSheetView.findViewById(R.id.file_name);
    file_name.setText("Вы хотите скачать файл " + filename + " ?");
    bottomSheetView.findViewById(R.id.file_name);
    bottomSheetView.findViewById(R.id.download_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            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);

            bottomSheetDialog.dismiss();

            Toast.makeText(MainActivity.this, filename +" скачивается...",
                            Toast.LENGTH_LONG).show();

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,
                            filename +" скачалось!",
                            Toast.LENGTH_SHORT).show();
                }
            }, 3000);

        }
    });
    bottomSheetDialog.setContentView(bottomSheetView);
    bottomSheetDialog.show();
}`[1]: https://i.stack.imgur.com/AWUAx.jpg
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...