Search code examples
androidandroid-download-manager

How to Download base64 image using DownloadManager?


How to Download base64 image using DownloadManager (I mean image must to show in application Downloads after download(convert base64 to bytes and save in file) )

For example: I need load image from my custom browser from next html "<img src=\"data:image/jpeg;base64,someBase64String" />". And load it exactly same(for user opinion) as a normal image url. For normal image url I use DownloadManager:

DownloadManager.Request request = new DownloadManager.Request(source);
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
        request.setTitle(fileName);
        request.setDescription(fileName);
        DownloadManager dm = (DownloadManager) App.getInstance().getSystemService(Context.DOWNLOAD_SERVICE);
        dm.enqueue(request);

Solution

  • DownloadManager dm = (DownloadManager) App.getInstance().getSystemService(Context.DOWNLOAD_SERVICE);
            String[] fileNameAndExt = getFileNameAndExt(file.getName());
            dm.addCompletedDownload(fileNameAndExt[0], file.getName(), true, "image/" + fileNameAndExt[1], file.getPath(), file.length(), true);
    

    Also need permission in Manifest:

    <uses-permission android:name="android.permission.INTERNET" />
    

    It is strange, but other side it not worked.