Android WebView üzerinde DownloadManager Kullanımı
Merhabalar bu makalemizde Android‘de kullandığımız WebView nesnesinin dosya indirmek amaçlı nasıl kullanabileceğine ilişkin bir örnek görmüş olacağız.
Bazı sayfalarınızda .pdf, .xlsx gibi dokümanları barındırabilir ve bu sayfaları linke tıklandığında indirilmesini sağlamanız gereken durumlar olabilir. Bu işlemin Java ve Kotlin dilleri üzerinde basitçe örnek kodlarını inceleyelim.
Var olan bir webview tanımlamasından sonra;
Kotlin dili için:
webView.setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
val request = DownloadManager.Request(
Uri.parse(url)
)
request.setMimeType(mimeType.toString())
val 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.toString()))
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType.toString()
)
)
val dm = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
dm.enqueue(request)
Toast.makeText(applicationContext, "Downloading File", Toast.LENGTH_LONG).show()
}
Java dili için:
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();
}});
request nesnesinin mimeType metodu ilgili indirilecek dosyanın hangi formatta olacağını belirtmektedir. setDescription; setNotificationVisibility özelliği ile indirme işlemi esnasında Notification üzerinde gösterilecek olan açıklama yazısını temsil ederken, setTitle ise bu bildirimin başlığını belirtir.
setDestinationInExternalPublicDir indirilecek dosyanın kaydedileceği konumun tanımlanması amacıyla kullanılmaktadır.
gerekli izinimizi AndroidManifest.xml de tanımlamayı unutmayalım;
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
iyi çalışmalar dilerim 🙂
Son Yorumlar