如何使用 DownloadManager 下载 base64 图像?

How to Download base64 image using DownloadManager?

如何使用 DownloadManager 下载 base64 图像(我的意思是图像必须在下载后显示在应用程序下载中(将 base64 转换为字节并保存在文件中))

例如: 我需要从下一个 html "<img src=\"data:image/jpeg;base64,someBase64String" />" 从我的自定义浏览器加载图像。并将其加载为与普通图像完全相同(出于用户意见)url。对于普通图像 url 我使用 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);

试试这个来下载图像。 uRl 是图片 url.

public void downloadFile(String uRl) {
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/MyBox");

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

        DownloadManager mgr = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

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

        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle("Demo")
                .setDescription("Something useful. No, really.")
                .setDestinationInExternalPublicDir("/MyFiles", "fileName.jpg");

        mgr.enqueue(request);

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

还需要清单中的权限:

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

这很奇怪,但另一面不起作用。