I have two fragments in a tab view layout. I am working with WebView() and DownloadManager() to download a file. My file is downloading perfectly but the downloaded file doesn't have the original file name. This is my problem. How do I get the original file name? I found some code here for this issue, but none of them helped me...
Here is my fragment where I am using the download manager:
public class Download extends Fragment {
View v;
WebView webView2;
SwipeRefreshLayout mySwipeRefreshLayout;
DownloadManager downloadManager;
public String currentUrl = "";
String myLink = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.download, container, false);
mySwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swiperefresh);
webView2 = (WebView) v.findViewById(R.id.webView_download);
int permissionCheck = ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
webView2.setInitialScale(1);
webView2.getSettings().setJavaScriptEnabled(true);
webView2.getSettings().setLoadWithOverviewMode(true);
webView2.getSettings().setUseWideViewPort(true);
webView2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView2.setScrollbarFadingEnabled(false);
webView2.setVerticalScrollBarEnabled(false);
webView2.loadUrl(currentUrl);
webView2.setWebViewClient(new WebViewClient());
webView2.getSettings().setBuiltInZoomControls(true);
webView2.getSettings().setUseWideViewPort(true);
webView2.getSettings().setLoadWithOverviewMode(true);
webView2.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Bundle bundle = getArguments();
if (bundle != null) {
String value = getArguments().getString("link");
myLink = value;
webView2.loadUrl(myLink);
}
webView2.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));
downloadManager = (DownloadManager) getContext().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, "Youtube_Video" + ".mp4");
request.allowScanningByMediaScanner();
Long reference = downloadManager.enqueue(request);
Toast.makeText(getContext(), "Downloading...", Toast.LENGTH_LONG).show();
}
});
} else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
return v;
}
}