如何在Android的WebView中的Blob中读取数据? [英] How to read the data in a Blob in WebView on Android?

查看:557
本文介绍了如何在Android的WebView中的Blob中读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在浏览器上创建对象Blob的服务器,我想在Android应用程序的WebView中下载该对象.我尝试将请求重定向到浏览器实例,也尝试使用下载管理器执行此操作,但是它们似乎都不起作用(即使我在Chrome中打开同一页面,下载操作也可以在其中进行).

I have a server that creates an object blob on the browser and I want to download this within WebView in an Android app. I tried redirecting the request to the browser instance as well as using the download manager to do this, but neither of them seems to work (Even though if I open up the same page in Chrome, the download operation works there).

我尝试了以下代码,它引发错误:

I tried the following code,it throws error:

Android.content.ActivityNotFoundException:未找到处理意图的活动{act = android.intent.action.VIEW dat = blob:https%3A//111.111.111.111%3A8080/40b63131-63b1-4fa4-9451-c6297bbd111a"

Android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=blob:https%3A//111.111.111.111%3A8080/40b63131-63b1-4fa4-9451-c6297bbd111a"

编辑

Android.content.ActivityNotFoundException:未找到处理意图的活动{act = android.intent.action.VIEW dat = blob:


代码:


Code:

mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}
});

并抛出java.lang.IllegalArgumentException:只能下载HTTP/HTTPS URI错误:

and this throws java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs error:

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);

    }
});

我应该如何下载Blob?任何帮助,将不胜感激.

How should I be downloading the blob? Any help would be appreciated.

推荐答案

是的,应该可行.在您的情况下,URL如下所示:删除blob:前缀后,其余部分将被URL解码:

Yes, it should be possible. In your case, the URL looks like this, once the blob: prefix is removed, and the rest is URL decoded:

blob:http://digitalinsensu.com/0f0d6127-a0f1-44c3-af85-72f648258d6d

请注意,只有当用户在其设备上运行可响应请求的Web服务器时,该URL才有效.之类的内容,请参见此处有关详细信息.

Be aware that this URL will only work if the user has a web server running on their device, which responds to requests, which is unlikely in case of blob: URLs. If you want to want to do this, you'd need to remove the blob: first, using Java's String.replace(..) or similar. To decode the URL, use something like URLDecoder.decode(url, "UTF-8");, see here for details.

问题在于DownloadListener确实在期待URI(例如 http://server.com/file .pdf ).但是,用户单击的链接不会传递该格式的URI.传递的URL代表附加到浏览器DOM的Blob.结果,该解决方案将无法按设计工作.

The issue is that the DownloadListener is really expecting a URI (ex http://server.com/file.pdf). However the link that the user clicks on does not pass a URI in that format. The URL passed represents a blob that is attached to the DOM of the browser. As a result the solution will not work as designed.

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

You may have another option. It depends on how you are generating the bytes for the blob. You mentioned that you are doing that in Javascript. If so, I would skip using the Blob and URL.createObjectURL completely and write an Javascript/Native interface that will allow you transfer the bytes in chunks (n number of arrays of 255 bytes for example) to the Java code. This will keep the memory consumption down on the client.

我对如何创建接口有一个想法,但是我首先需要了解您如何为Blob生成字节.如果您可以发布字节数组,我可以再试一次.

I have an idea on how to create the interface but I first need to understand how you are generating the bytes for the blob. If you can post a byte array I may try further.

有关此检查的详细信息,请此处

For Full Detail on this check here

这篇关于如何在Android的WebView中的Blob中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆