-2

I use this code for download link from url and save to sd card ( whith smartphone )

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://yourname.com/file.mp3"));
startActivity(intent);

But after start this intent , browser open and after download link

How download from url without open browser ?

user3612383
  • 88
  • 2
  • 10
  • Best answer for Your Questions [http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog][1] – Ahmad Aghazadeh Jun 29 '15 at 07:06

2 Answers2

1

If you want to download in background then use DownloadManager, because using intent will always launch browser in foreground. A simple tutorial for using DownloadManager is

http://blog.vogella.com/2011/06/14/android-downloadmanager-example/

Or you can also look here

http://www.compiletimeerror.com/2013/11/download-manager-in-android-with-example.html#.VZDtUfmqqko
Adi Tiwari
  • 761
  • 1
  • 5
  • 17
0

Try this code place this function in your Activity and pass required parameter and this function start download process automatically and after save this function retutn you downloaded file as a FILE.

public File downloadFile(Context context, String url, String filename) {
            File direct = new File(Environment.getExternalStorageDirectory() + "/" + "Your_Foldername");

            if (!direct.exists()) {
                direct.mkdirs();
            } else {
                File file = new File(Environment.getExternalStorageDirectory() + "/" + "Your_Foldername" + "/" + filename);
                if(file.exists())
                file.delete();
            }

            DownloadManager mgr = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);

            Uri downloadUri = Uri.parse(url);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);

            request.setAllowedNetworkTypes(
                    DownloadManager.Request.NETWORK_WIFI
                            | DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false).setTitle(context.getResources().getString(R.string.app_name))
                    .setDescription("Downloading Share Image for " + context.getResources().getString(R.string.app_name))
                    .setDestinationInExternalPublicDir("/" + "Your_Foldername", filename);

            mgr.enqueue(request);
            return direct;
        }
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38