-2

I have seen many tutorials for android download manager and every one is same.but promblem is that I have used this code in my app after using, but whenever I click this button my app is crashing. my version is android marshmallow and my code is here. also I want to ask I have three permissions in manifest of internet network and external storage. I also want to know that "is it possible to save download file in my app folders" if yes, how

public class Dashboard extends AppCompatActivity {
    Button btn;

Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    Button btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View view){
        String url = "http://www.myweb.com/abc.png";
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

// only download via WIFI
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
            request.setTitle("Example");
            request.setDescription("Downloading a very large zip");

// we just want to download silently
            request.setVisibleInDownloadsUi(false);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            request.setDestinationInExternalFilesDir(context, null, "large.zip");

// enqueue this request
            DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            long downloadID = downloadManager.enqueue(request);
    }
    });
}

}
m bilal
  • 11
  • 4

1 Answers1

0

I have found two bugs in your code. First one is url showing 404. Another one is yor are not giving download directory.

Try below snippet of code - Hope it will solve your problem.

public class Dashboard extends AppCompatActivity {
        Button btn;

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);

        Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick (View view){
            String url = "http://www.myweb.com/abc.png"; // Please use a valid url. This url is says 404 not found.
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

// only download via WIFI
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.setTitle("Example");
        request.setDescription("Downloading a very large zip");

// we just want to download silently
        request.setVisibleInDownloadsUi(false);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        request.setDestinationInExternalFilesDir(context, Environment.getExternalStorageDirectory().getAbsolutePath(), "2.gif");

// enqueue this request
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        long downloadID = downloadManager.enqueue(request);
        }
        });
    }

    }
Al-Amin
  • 1,369
  • 1
  • 9
  • 26
  • request.setDestinationInExternalFilesDir(context, context.getFilesDir().getAbsolutePath(), "large.zip"); // If you want to save your file in internal storage set getFilesDir() – Al-Amin Oct 18 '18 at 04:59
  • Al Amin thank you for your help. but app still crashing with this code.i have copy paste your code. thank you – m bilal Oct 18 '18 at 07:25
  • cometonice.com/2.gif – m bilal Oct 18 '18 at 07:27
  • I have edited my answer take a look. You need to provide file extension as in your download url - like .gif, mp3 etc. Please upload your crash report. – Al-Amin Oct 18 '18 at 07:55