Search code examples
androidandroid-download-manager

Android DownloadManager how to mark file as temporary before finishing downloading


I have a DownloadManager to download video so that I can use player to play. But when I start to download, it has generated the unfinished .mp4 file, so when I tried to play before finish downloading, it will show error, is that possible change its extension before finish downloading?

DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);    
Uri uri = Uri.parse(url);    
DownloadManager.Request request = new DownloadManager.Request(uri);    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4");
long id = manager.enqueue(request);

Solution

  • You can solve your problem in a little different way
    Instead of change its extension before finish downloading you need to start the download with the changed extension like this:

    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4.tmp");
    

    and once download is finished, rename file to "/Android/data/", id + ".mp4"