Search code examples
androidandroid-fragmentsandroid-fileandroid-download-manager

DownloadManager not working inside Fragment in Android


I am trying to download a file using DownloadManager

The same code works in Activity but doesn't work in Fragment. What's the problem? Text below might be the problem but please review the whole code

in Activity i used DownloadManager manager = (DownloadManager) getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE);

In Fragment i used DownloadManager manager = (DownloadManager) getActivity().getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE); inside download.setOnClickListener()

 String myHTTPurl = "http://status9.in/ankushfiles/it2.html";
  File file;
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String title = getTitle();
    webView = (WebView) getActivity().findViewById(R.id.webView);
    download = (Button) getActivity().findViewById(R.id.bDownloadTT);


    file = new File(Environment.getExternalStoragePublicDirectory("/sdcard/KiiTTimeTableData"), "it2.html");
    if(file.exists())
    {
        Toast.makeText(getActivity(), "File Exists", Toast.LENGTH_SHORT).show();
        webView.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/sdcard/KiiTTimeTableData/it2.html");
    }
    else
    {
        Toast.makeText(getActivity(), "File Does Not Exist", Toast.LENGTH_SHORT).show();
        webView.setVisibility(View.INVISIBLE);
    }

    download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            file.delete();
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myHTTPurl));
            request.setTitle("File Download");
            request.setDescription("Downloading....");

            //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            String nameOfFile = URLUtil.guessFileName(myHTTPurl, null, MimeTypeMap.getFileExtensionFromUrl(myHTTPurl));

            request.setDestinationInExternalPublicDir("sdcard/KiiTTimeTableData", nameOfFile);
            DownloadManager manager = (DownloadManager) getActivity().getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
        }
    });

}

Solution

  • In fragment i think you should initialze and return the Views in onCreateView method. Read this follwing articles:

    Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments When to use onCreateView for fragments?

    And what is your errors log?

    Are you sure this two following permissions have been declared?

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />