I am trying to download a file stored in a cloud storage site internxt using file url from a webview in android. file is opening in webview. but unable to download. app stopped working when clicked download & downloaded 100%.
Logcat showing error like following java.lang.IllegalArgumentException: Can not handle uri:: blob:https://share.internxt.com/38f153b9-c64e-4516-9335-f75f59137546
please help me to download the file
Here is my source code
public class HomeFragment extends Fragment {
String URL = "http://nregsorders.co.in/index.php";
private FragmentHomeBinding binding;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//View view1 = inflater.inflate(R.layout.fragment_home, container, false);
WebView web = (WebView) view.findViewById(R.id.Webview1);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setPluginState(WebSettings.PluginState.ON);
web.getSettings().setDomStorageEnabled(true);
web.setWebViewClient(new WebViewClient());
web.loadUrl(URL);
web.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
// Intent i = new Intent(Intent.ACTION_VIEW);
// i.setData(Uri.parse(url));
// startActivity(i);
DowloadDialog(url,userAgent,contentDisposition,mimetype);
}
});
}
public void DowloadDialog(final String url, final String UserAgent, String contentdisposition, String mimetype)
{
//Toast.makeText(requireView().getContext(), url, Toast.LENGTH_LONG).show();
final String filename = URLUtil.guessFileName(url,contentdisposition,mimetype);
final AlertDialog.Builder builder = new AlertDialog.Builder(requireView().getContext());
builder.setTitle("Downloading...")
.setMessage("Do you want to Download "+ ' '+" "+filename+" "+' ')
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
DownloadManager manager = (DownloadManager) getActivity().getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
long reference = manager.enqueue(request);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.show();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(URL)) {
// This is your web site, so do not override; let the WebView to load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
// this will ignore the Ssl error and will go forward to your site
handler.proceed();
}
}
}
i just need to download the file when clicking the download button from the webview link