Estoy usando webview en una aplicación de Android. Estoy tratando de descargar un archivo .pdf , sin embargo, cuando se hace clic en el enlace a través de la aplicación, el nombre del archivo .pdf cambia a "1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf.pdf" y no se guarda con el nombre del archivo original.
¿Cómo hacer que webview guarde el archivo con el nombre original? En este momento, la vista web está guardando el archivo usando la ID como nombre.
Enlace usado: https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf
WebView:
webView.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(); }});Permisos en Manifiesto:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>Aquí una prueba con su enlace (en Kotlin). Si necesita un ejemplo de Java, hágamelo saber:
private fun test() { webView = findViewById(R.id.webView) webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { Log.d(TAG, "shouldOverrideUrlLoading:url = ${url}") if (url.contains("=download")){ Log.d(TAG, "shouldOverrideUrlLoading: ") downloadFile(url) webView.stopLoading() } return true } } val url = "https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf" webView.loadUrl(url) } fun downloadFile(url: String) { Log.d(TAG, "downloadFile: url = $url") val manager = getSystemService(Activity.DOWNLOAD_SERVICE) as DownloadManager val uri = Uri.parse(url) val request = DownloadManager.Request(uri) request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) val reference: Long = manager.enqueue(request) }Código Java:
private void test() { webView = findViewById(R.id.webView); webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.contains("=download")){ downloadFile(url); } return super.shouldOverrideUrlLoading(view, url); } }); String url = "https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf"; webView.loadUrl(url); } private void downloadFile(String url) { DownloadManager manager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE); Uri uri = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(uri); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); manager.enqueue(request); }Una opción para usar DownloadManager
Para esto, debe agregar un evento onNavigating a su WebView. Si el usuario apunta a un archivo pdf, puede detener el proceso de carga con: ags.Cancel = true Y este punto cuando puede pasar la URL a un DownloadManager que descargará su archivo.
Aquí un código simple
webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { if (url.contains(".pdf")){ downloadFile(url) } return false } } fun downloadFile(url: String) { Log.d(TAG, "downloadFile: url = $url") val manager = getSystemService(Activity.DOWNLOAD_SERVICE) as DownloadManager val uri = Uri.parse(url) val request = DownloadManager.Request(uri) request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) val reference: Long = manager.enqueue(request) }