在Android Lollipop网页视图中,我想让用户下载生成的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方面,我写了一个试图使用DownloadManager下载文件的DownloadListener:
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;
}
}
当我点击网页视图中的链接时,会出现此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)。
我可以将数据传递给注入addJavascriptInterface的对象,但是我必须base64 encode my blob并在java端进行解码(因为只能传递基元和简单类型)。通过javascript编码到base64会导致Chromium崩溃50MB blob(调用readAsDataURL时出现内存错误)。
我还能从网页视图下载50MB blob吗?或者将50MB二进制数据从javascript传输到Android java?
==使用案例详情==
我正在使用javascript生成一个带有mime类型video/x-ms-wmv
的50MB视频文件blob。这一代完全是客户端。由于我可以通过object URL在桌面浏览器(以及普通的Chrome应用)上下载文件,因此我确信该步骤有效。然后我想以某种方式让用户将该文件存储在他/她的外部存储器上(可能在DCIM或下载中)。
在一个不相关的说明中,我还想对音频WAV文件做同样的事情(比如生成铃声以存储在铃声中)。
答案 0 :(得分:3)
您可能有其他选择。这取决于您如何为blob生成字节。你提到你在Javascript中这样做。如果是这样,我将完全跳过使用Blob和URL.createObjectURL,并编写一个Javascript / Native接口,允许您将块中的字节(例如,数量为255个字节的数组)传输到Java代码。这将使客户端的内存消耗降低。
我知道如何创建接口,但我首先需要了解如何为blob生成字节。如果你可以发布一个字节数组,我可以使用它来测试我的解决方案。让我知道。