dan*_*007 22 android android-webview
在Android Lollipop Web视图中,我想让用户下载生成的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);
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
Run Code Online (Sandbox Code Playgroud)
单击Web视图中的链接时会出现此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)
Run Code Online (Sandbox Code Playgroud)
当我在HTTP服务器上托管我的页面时,我得到了同样的错误,所以错误似乎源于该blob:部分.
我现在有什么选择?如何下载存储在对象URL中的数据?最终,我想下载更大的blob(~50MB).
我可以将数据传递给注入addJavascriptInterface的对象,但是我必须对java blob进行base64编码并在java端进行解码(因为只能传递基元和简单类型).通过javascript编码到base64会导致Chromium崩溃50MB blob(调用readAsDataURL时出现内存错误).
我怎么能从网络视图下载50MB blob?或者将50MB二进制数据从javascript传输到Android java?
==用例详情==
我正在使用javascript生成一个带有mime类型的50MB视频文件blob video/x-ms-wmv.这一代完全是客户端.由于我可以通过对象URL在桌面浏览器(以及普通的Chrome应用程序)上下载文件,因此我确信步骤有效.然后我想以某种方式让用户将该文件存储在他/她的外部存储器上(可能在DCIM或下载中).
在一个不相关的说明中,我还想对音频WAV文件做同样的事情(比如生成铃声以存储在铃声中).
大卫,我已经调查过了。你在这里面临着一个很好的挑战。问题是 DownloadListener 实际上需要一个 URI(例如http://server.com/file.pdf)。但是,用户单击的链接不会传递该格式的 URI。传递的 URL 表示附加到浏览器 DOM 的 blob。因此,该解决方案将无法按设计工作。
您可能还有另一种选择。这取决于您如何为 blob 生成字节。你提到你正在用 Javascript 来做这件事。如果是这样,我会完全跳过使用 Blob 和 URL.createObjectURL 并编写一个 Javascript/Native 接口,该接口允许您将字节以块的形式(例如 n 个 255 字节的数组)传输到 Java 代码。这将降低客户端的内存消耗。
我对如何创建接口有一个想法,但我首先需要了解如何为 blob 生成字节。如果您可以发布字节数组,我可以用它来测试我的解决方案。让我知道。
| 归档时间: |
|
| 查看次数: |
2185 次 |
| 最近记录: |