如何使用DownloadManager下载base64图像?

时间:2017-02-09 05:33:10

标签: android android-download-manager

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

例如: 我需要从下一个html "<img src=\"data:image/jpeg;base64,someBase64String" />"的自定义浏览器加载图片。并将其加载为与普通图像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);

2 个答案:

答案 0 :(得分:1)

试试这个下载图片。 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);

    }

答案 1 :(得分:0)

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

还要在Manifest中获得许可:

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

这很奇怪,但是其他方面没有用。