M.Honi
M.Honi

Reputation: 133

Download Manager not working

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

Upvotes: 7

Views: 10900

Answers (4)

A.Shah
A.Shah

Reputation: 1

request.setDestinationInExternalPublicDir("/BlueNet/", fileName);

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

Upvotes: 0

Tohid Noori
Tohid Noori

Reputation: 60

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"));

Upvotes: 1

Savej Saifi
Savej Saifi

Reputation: 1

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

Just remove this line or make directory in another way.

Upvotes: 0

Grender
Grender

Reputation: 1619

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.

Upvotes: 4

Related Questions