Download Images and mp3 with webview Android - java

I need to Download images(png,jpg,jpeg,gif) and mp3 in Android webView.
When i click download mp3 button or image button it not give any response.
Below is my code
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
if(webview.getUrl().contains(".mp3")){
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download mp3");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
if(url.indexOf("somesite.com") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Im using api levels from 21.
i also have storage read write permission in manifest.
Kindly Help

Related

How to download and immediately open this file in another application

Good day. I am creating an application that downloads a file from firebase. Next, this file will have to open in the application that is installed in advance on the phone. How can I find this file after installation and open it in a new application. Thanks for any answer. sorry for my English
Download code:
public void downloadFiles(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
downloadManager.enqueue(request);
What i tried.
open code:
public void openFile() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("file/*");
File download = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"king.mcpack");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(download));
startActivity(Intent.createChooser(intent, "Open with"));
}

Unable to download the pdf file from WebView. Always download .bin file. I checked in one plus mobile having android 10

I try to download file inside a WebView. File is downloaded using browser but when I try to download through mobile WebView. It is download as .bin file. I checked this in android 10 device. It is working in below android 10. I was using Advance WebView library to open the WebView. I am using below code for that. Any help will be highly appreciated.
#Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
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();
Log.d("dsfgh", "onDownloadRequested: "+url);
}
In Kotlin, Work For Me
// coding download
webView.setDownloadListener { url, userAgent, contentDisposition, mimetype, _ ->
//checking Runtime permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//Do this, if permission granted
downloadDialog(url, userAgent, contentDisposition, mimetype)
} else {
//Do this, if there is no permission
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
100
)
}
} else {
//Code for devices below API 23 or Marshmallow
downloadDialog(url, userAgent, contentDisposition, mimetype)
}
}
}
private fun downloadDialog(url: String?, userAgent: String?, contentDisposition: String?, mimetype: String?) {
//getting file name from url
val filename = URLUtil.guessFileName(url, contentDisposition, mimetype)
//Alertdialog
val builder = AlertDialog.Builder(this)
//title for AlertDialog
builder.setTitle("Download")
//message of AlertDialog
builder.setMessage("Do you want to save $filename")
//if YES button clicks
builder.setPositiveButton("Yes") { _, _ ->
//DownloadManager.Request created with url.
val request = DownloadManager.Request(Uri.parse(url))
//cookie
val cookie = CookieManager.getInstance().getCookie(url)
//Add cookie and User-Agent to request
request.addRequestHeader("Cookie", cookie)
request.addRequestHeader("User-Agent", userAgent)
//file mimetype
request.setMimeType(mimetype)
request.setTitle(URLUtil.guessFileName(url,contentDisposition,mimetype))
//file scanned by MediaScannar
request.allowScanningByMediaScanner()
//Download is visible and its progress, after completion too.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
//DownloadManager created
val downloadmanager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
//Saving file in Download folder
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url,contentDisposition,mimetype))
//download enqued
downloadmanager.enqueue(request)
}
//If Cancel button clicks
builder.setNegativeButton("Cancel")
{ dialog, _ ->
//cancel the dialog if Cancel clicks
dialog.cancel()
}
val dialog: AlertDialog = builder.create()
//alertdialog shows
dialog.show()
}

Download Manager is not showing file in Gallery after downloading from webview

I want to download image from my webview, a notification comes and it downloads the file but when i go to Gallery then the file is not there.What i'm doing wrong ... ??
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(geturl));
request.setTitle("File Download");
request.setDescription("File is being downloaded...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameOfFile = URLUtil.guessFileName(geturl, null, MimeTypeMap.getFileExtensionFromUrl(geturl));
request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DCIM,nameOfFile);
DownloadManager manager = (DownloadManager)getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
While geturl is the Override url by which i am taking the url of an image.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Get url for downloading
geturl = url;
return true;
}

How can I open play store market:// in webview [duplicate]

I have some links in my webview that are market:// links. When my users tap on them, it gives them a page cannot be found error.
How can I allow all links that begin with market:// to automatically open the Google play store when they are tapped? I tried:
final Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("market://details?id="));
startActivity(intent);
}
but that didn't seem to do anything. I am pretty new to this so any help would be appreciated. Also, FYI, I cannot change the market:// links to play.google.com myself. They are from my advertiser.
Is there anyway I can include it in this code:
public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
if (DEBUG)
Log.e("shouldOverride", paramString);
if (Uri.parse(paramString).getHost()!=null && (!Uri.parse(paramString).getHost().equals("market.android.com")) && (!paramString.contains("facebook.com")) && (!Uri.parse(paramString).getHost().contains("twitter.com")) && (!Uri.parse(paramString).getHost().equals("play.google.com"))
&& (!Uri.parse(paramString).getHost().contains("bit.ly")) && (!Uri.parse(paramString).getHost().contains("plus.google.com")) && (!Uri.parse(paramString).getHost().contains("youtube.com"))){
if(isAppOrGamePage(paramString)){
final Intent intent = new Intent(MainActivity.this, PageActivity.class);
intent.putExtra("app_url", paramString);
startActivity(intent);
} else
return false;
} else {
final Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(paramString));
startActivity(intent);
}
return true;
}
}
You can decide what to do by looking the scheme of the url, if Google Play Store app is installed you can open the detail page in Play Store app, else you can show Google Play web page of the application
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
});
you can use this code like this also if its help you:
// It will not work in android simulator as it does not have Google Play Store
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+APP_ID)));
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}

PDF intent in android

while developing an android app , I am getting error "sorry we were unable to find the document at the original source" while using the following code : please advise
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
if (url.contains(".pdf")){
Toast.makeText(view.getContext(), "chand", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
view.getContext().startActivity(intent);
//String googleDocs = "http://docs.google.com/gview?embedded=true&url=";
//view.loadUrl(googleDocs + url);
//Toast.makeText(view.getContext(), "chand", Toast.LENGTH_LONG).show();
//String pdfurl = "http://docs.google.com/gview?embedded=true&url=" + url;
//Log.i(TAG, "Opening PDF: " + url);
//view.getSettings().setJavaScriptEnabled(true);
//view.loadUrl(pdfurl);
}
return false;
}
To use that intent you need to download the PDF to the device onto the sdcard then give the uri to the pdf on the sd card in the intent.

Categories

Resources