1

I start developing my very first Android project. In my project, I need to download media files, especially mp3 or mp4. I am downloading file using DownloadManager.

Here is my download code for mp3

private void downloadPodcast(int id)
    {
        String url = context.getResources().getString(R.string.api_endpoint)+"podcast/download?id="+String.valueOf(id);

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription("Downloading...");
        request.setTitle("Podcast");
        request.setMimeType("audio/MP3");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
        DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }

As you can see in my code, I am downloading only mp3 and setting the MIME type is constant. The file name and its extension is constant as well. What I want to do is to detect the file extension I will download. So can I set the MIME type programmatically and set the extension of file name. How can I achieve it in DownloadManager?

halfer
  • 18,701
  • 13
  • 79
  • 158
Wai Yan Hein
  • 9,841
  • 21
  • 103
  • 244
  • see this http://stackoverflow.com/questions/8589645/how-to-determine-mime-type-of-file-in-android – The_ehT Feb 05 '16 at 09:41
  • But my url format is somthing like this www.example.com/download?id=1 . I means, not end with extension like .mp3 or .mp4 . I want to detect extension something like file upload in web . How can I know the original downloaded file name as well ? – Wai Yan Hein Feb 05 '16 at 09:48
  • Read `Content-Type` Header as shown in http://stackoverflow.com/a/9078217/1517029. If you want more example on how to read httpresponse see here(http://www.mkyong.com/java/how-to-get-http-response-header-in-java/). If still need help then I will write in answer. – The_ehT Feb 05 '16 at 09:53
  • Throwing exception always when I try to get response – Wai Yan Hein Feb 05 '16 at 10:04
  • I had to use try catch for response. But it is always in catch. But when I log message in catch. It is empty – Wai Yan Hein Feb 05 '16 at 10:27
  • Did you import the libraries correctly? – The_ehT Feb 05 '16 at 10:42
  • I used URLConnection . Cause apache is not supported anymore. – Wai Yan Hein Feb 05 '16 at 10:47
  • That is already included in my current version – Wai Yan Hein Feb 05 '16 at 10:47
  • Well I can't help if I don't know what is error log being shown. – The_ehT Feb 05 '16 at 10:52
  • From my observations, if you do not call `setMimeType` and `setDestinationInExternalPublicDir`, android will download file into global download cache (viewable through built-in "Downloads" app) and deduce correct file extension. After this you could probably copy the file to your required destination and remove it from the global cache (if necessary). – Stan Apr 10 '16 at 21:32

1 Answers1

2

Though its quite late, however here is the answer:

You can get the extension of the file to download using the code below and add the extension to your file name.

String fileUrl = "http://someurl";
String fileName = "foobar";

String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);

// concatinate above fileExtension to fileName
fileName += "." + fileExtension;

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl))
                .setTitle(context.getString(R.string.app_name))
                .setDescription("Downloading " + fileName)
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        dm.enqueue(request);