7

I'm trying to develop app that show videos and you can Download it i'm using Download Manager class but it didn't work, also it didn't give me any error :(

this is my download manager code:

    public void downloadFileFromUrl(String url, String fileName) {

        String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";

        File folder = new File(filePath);

        if (!folder.exists()) {
            folder.mkdirs();
        }

        try {

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

        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.allowScanningByMediaScanner();

        request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setVisibleInDownloadsUi(true);
        DownloadManager downloadManager = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
        long id= downloadManager.enqueue(request);
            Toast.makeText(this, fileName, Toast.LENGTH_LONG).show();
            Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();

        }

        catch (Exception ex){
            Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
        }
    }

and this is how I'm calling it

downloadFileFromUrl(path, fileName);

where:

path: "192.168.1.5:8080/BlueNet_NMC/blue_elephant.mp4"

filename: "blue_elephant.mp4"

and i already give this permissions to manifests

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

so please any help

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
M.Honi
  • 123
  • 1
  • 1
  • 12
  • Have you tried "/BlueNet" instead of "/BlueNet/"? Also, AFAIK there's no need to check if that folder exists, `DownloadManager` does it for you. – Grender May 06 '16 at 22:56
  • ok i delete check steep – M.Honi May 06 '16 at 22:58
  • Have you tried to download an image or something from the Internet instead of a file from your local network? – Grender May 06 '16 at 22:59
  • no, but when i put my link in browser i can download it – M.Honi May 06 '16 at 23:00
  • Just try it and tell me if it works. – Grender May 06 '16 at 23:01
  • OMG, it's working with link from internet, so what's the problem and the other issue that the download arrow doesn't appear – M.Honi May 06 '16 at 23:12
  • `DownloadManager` only handles http/https requests. It's weird the download arrow should be appearing, try not to allow the check by media scanner or try a different `Visibility`. – Grender May 06 '16 at 23:17
  • but my link also http "http:// 192.168.1.5:8080/BlueNet_NMC/video_share/blue_elephant.mp4" – M.Honi May 06 '16 at 23:22
  • i made space between http and 192 to you can see it – M.Honi May 06 '16 at 23:23
  • Maybe it's not using HTTP protocol to transfer the file in your local network. Which server are u using? – Grender May 06 '16 at 23:26
  • i'm using wamp server on my laptop – M.Honi May 06 '16 at 23:28
  • So I don't know exactly why it's not working with your local network url, I should check more thigns but I think it's a common issue. – Grender May 06 '16 at 23:32
  • ok, please put your answer to mark it as true answer thank you :) – M.Honi May 06 '16 at 23:36
  • @M.Honi I think that Download Manager *does* support HTTPS, take a look at [this](https://developer.android.com/reference/android/app/DownloadManager.Request?hl=en#public-constructors_2) (I'm still trying to successfully download an image that way though, but I've been at it only for some hours as of now) – DarkCygnus May 21 '18 at 23:03
  • it's downloading, but finally it's getting failed to download. file doesn't exist at specified directory. – Ashish Patel Aug 10 '19 at 07:55

4 Answers4

3

As I said in the comments, DownloadManager only handles requests starting with http:// or https:// as you can see in the docs.

I don't know exactly what's the problem because I lack information about your server, but I think it's a common issue, so you should avoid using an IP address without providing that scheme.

Grender
  • 1,589
  • 2
  • 17
  • 44
  • Sorry to contradict, but it seems that HTTPS is indeed supported. Take a look at [this link](https://developer.android.com/reference/android/app/DownloadManager.Request?hl=en#public-constructors_2), where they indicate: `Uri: the HTTP or HTTPS URI to download. ` – DarkCygnus May 21 '18 at 22:59
  • I didn't mean to say that, I have updated my answer to reflect what I wanted to say. Thanks for pointing that out, @DarkCygnus! – Grender May 22 '18 at 13:30
1

I had a problem when downloading files with an HTTP URL using the DownloadManger class; but then I did the following and the problem was fixed.

Instead of this code:

String url = "http://masteranime.ir/music/best/Dragon Ball GT Dan Dan Kokoro Hikareteku.mp3";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

use this code:

String url = "http://masteranime.ir/music/best/Dragon Ball GT Dan Dan Kokoro Hikareteku.mp3";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url).replaceAll(" ","%20"));
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

You have problem in this line - request.setDestinationInExternalPublicDir("/BlueNet/",fileName);

Just remove this line or make directory in another way.

Style-7
  • 985
  • 12
  • 27
0
request.setDestinationInExternalPublicDir("/BlueNet/", fileName);

You have to mention directory as first argument here. /BlueNet/ is not a directory.

Giorgio
  • 1,973
  • 4
  • 36
  • 51
A.Shah
  • 1
  • 1