android - 在 Lollipop Web View 中下载对象 URL?

标签 android android-webview

在 Android Lollipop 网页 View 中,我想让用户下载生成的 txt 文件:

// Store some text in a data URL.
var dataUrl = (window.URL || window.webkitURL).createObjectURL(
    new Blob(["Hello world. :)"]));

// Create a link that lets the user download the text file as hello.txt.
var downloadLink = document.createElement('a');
downloadLink.setAttribute('href', dataUrl);
downloadLink.innerHTML = 'Click to download.';

// Working with David on this. I did the same thing.
// Fyi, setting the 'download' attribute just makes hitting the link
// nop, ie. do nothing at all in the web view, so I omitted it.
// - John    

// Display the link.
document.getElementById('container').appendChild(downloadLink);

在Android java端,我写了一个DownloadListener尝试使用 DownloadManager 下载文件:

package com.somesideprojects;

import android.webkit.DownloadListener;


/**
 * A download listener that lets users download files from the web view.
 */
public class CustomDownloadListener implements DownloadListener {

    private MainActivity currentActivity;

    @Override
    public void onDownloadStart(
            String url,
            String userAgent,
            String contentDisposition,
            String mimeType,
            long contentLength) {
        android.util.Log.d("Logger",
                "url : " + url +
                " userAgent: " + userAgent +
                " contentDisposition: " + contentDisposition +
                        " mimeType: " + mimeType + " contentLength " + contentLength);

        android.net.Uri source = android.net.Uri.parse(url);

        // Make a new request.
        android.app.DownloadManager.Request request =
                new android.app.DownloadManager.Request(source);

        // Appears the same in notification bar while downloading.
        String filename = getFilename(contentDisposition);

        request.setDescription(
                "This project will be saved in your downloads folder as " + filename + ".");
        request.setTitle(filename);

        // Add cookie on request header (for authenticated web app).
        String cookieContent = getCookieFromAppCookieManager(source.getHost());
        request.addRequestHeader("Cookie", cookieContent);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(
                    android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }

        // Save the file in the "Downloads" folder of SDCARD.
        request.setDestinationInExternalPublicDir(
                android.os.Environment.DIRECTORY_DOWNLOADS, filename);

        // Get the download service and enqueue the file.
        android.app.DownloadManager manager =
                (android.app.DownloadManager) this.currentActivity.getApplication()
                        .getSystemService(android.content.Context.DOWNLOAD_SERVICE);

        manager.enqueue(request);
    }

    public String getFilename(String contentDisposition){
        String filename[] = contentDisposition.split("filename=");
        return filename[1].replace("filename=", "").replace("\"", "").trim();
    };

    public String getCookieFromAppCookieManager(String url){
        android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
        if (cookieManager == null) {
            return null;
        }
        String rawCookieHeader = null;

        // Extract Set-Cookie header value from Android app CookieManager for this URL
        rawCookieHeader = cookieManager.getCookie(url);
        if (rawCookieHeader == null) {
            return null;
        }

        return rawCookieHeader;
    };

    public void setCurrentActivity(MainActivity currentActivity) {
        this.currentActivity = currentActivity;
    }
}

当我单击 Web View 中的链接时,会出现此 java 错误:

 java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: blob:file%3A///f566c1cf-b0b2-4382-ba16-90bab359fcc5
            at android.app.DownloadManager$Request.<init>(DownloadManager.java:429)

当我在 HTTP 服务器上托管我的页面时,我遇到了同样的错误,因此该错误似乎源于 blob: 部分。

我现在有什么选择?如何下载存储在对象 URL 中的数据?最终,我想下载一个更大的 blob (~50MB)。

我可以将数据传递给注入(inject)到 addJavascriptInterface 中的对象。 ,但我必须 base64 encode my blob并在 java 端解码(因为只能传递原语和简单类型)。通过 javascript 编码为 base64 会使 Chromium 崩溃 50MB blob(调用 readAsDataURL 时出现内存不足错误)。

我还能如何从 Web View 下载 50MB 的 blob?或者将 50MB 二进制数据从 javascript 传输到 Android java?

== 用例详情 ==

我正在使用 javascript 生成一个 50MB 的视频文件 blob,mime 类型为 video/x-ms-wmv。这一代完全是客户端。我确信该步骤有效,因为我可以通过 object URL 在桌面浏览器(以及普通的 Chrome 应用程序)上下载文件。 .然后我想以某种方式让用户将该文件存储在他/她的外部存储中(可能在 DCIM 或下载中)。

另外,我还想对音频 WAV 文件做同样的事情(比如生成铃声以存储在铃声中)。

最佳答案

大卫,我已经调查过了。你在这里有一个很好的挑战。问题是 DownloadListener 确实需要一个 URI(例如 http://server.com/file.pdf )。但是,用户单击的链接不会传递该格式的 URI。传递的 URL 表示附加到浏览器 DOM 的 blob。因此,该解决方案将无法按设计工作。

您可能还有其他选择。这取决于您如何为 blob 生成字节。您提到您正在使用 Javascript 执行此操作。如果是这样,我将完全跳过使用 Blob 和 URL.createObjectURL 并编写一个 Javascript/Native 接口(interface),它允许您将字节 block (例如,n 个 255 字节的数组)传输到 Java 代码。这将降低客户端的内存消耗。

我对如何创建接口(interface)有一个想法,但我首先需要了解如何为 blob 生成字节。如果您可以发布一个字节数组,我可以使用它来测试我的解决方案。告诉我。

关于android - 在 Lollipop Web View 中下载对象 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31113160/

相关文章:

android - React-Native 构建失败的应用程序 :mergeDebugAssets

android - 将 Robolectric 与 Gradle 结合使用时的 Resources$NotFoundException

android - 如何对齐单选组中的单选按钮

javascript - 适用于 Web 应用的 Cloud Firestore

android - 清除 AdMob 在 Android 中生成的缓存文件是一种好习惯吗?

javascript - 在不禁用 Javascript 的情况下在 WebView 中禁用 HTML5

java - Android软键盘调整大小布局

java - 如何从另一个类读取变量?

android - 如何让 Android WebView 不忽略 'height' css 属性?

android - View.LAYER_TYPE_HARDWARE 使 NestedScrollView 内的 WebView 崩溃