通过内容提供商传递二进制数据 [英] Passing binary blob through a content provider

查看:122
本文介绍了通过内容提供商传递二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内容提供商的习惯我的一套Android应用程序,以及它需要公开的事情之一是小(20-30昆明植物研究所)字节数组。这些斑点的URI是这样的:

I have a content provider that is custom to my set of Android applications, and one of the things it needs to expose is a small (20-30 KiB) byte array. The URI for these blobs looks like:

content://my.authority/blob/#

其中,是行号;在游标的最终拥有标准 _id 列和数据列。我使用的是 MatrixCursor 以提供程序的查询()方法:

where # is the row number; the resulting cursor has the standard _id column and a data column. I'm using a MatrixCursor in the provider's query() method:

byte[] byteData = getMyByteData();
MatrixCursor mc = new MatrixCursor(COLUMNS);
mc.addRow(new Object[] { id, byteData });

后来,在消费数据的应用程序,我做的:

Later, in the application consuming the data, I do:

Cursor c = managedQuery(uri, null, null, null, null);
c.moveToFirst();
byte[] data = c.getBlob(c.getColumnIndexOrThrow("data"));

然而,数据不包含我的原始字节数组的内容;相反,它包含有类似 [B @ 435cc518 ,它看起来更像是比数组内容的地址。我试着包裹字节数组中的实施的java.sql.Blob ,搞清楚它可能会寻找,由于内容提供商子系统写很容易与使用SQLite的,但它并没有帮助。

However, data does not contain the contents of my original byte array; rather, it contains something like [B@435cc518, which looks more like the address of the array than the contents. I tried wrapping the byte array in an implementation of java.sql.Blob, figuring that it might be looking for that since the content provider subsystem was written to be easy to use with SQLite, but it didn't help.

有没有人得到这个工作?如果数据是在文件系统中,也有的ContentProvider 的方法,我可以用它来提供一个编组的InputStream 来在客户端,但数据我想寄回生活作为内容提供商的APK资源。

Has anyone gotten this to work? If the data was in the file system, there are methods in ContentProvider that I could use to provide a marshalled InputStream to the client, but the data I'm trying to send back lives as a resource in the content provider's APK.

推荐答案

您将无法使用 MatrixCursor 发送字节数组。这是因为它依赖于 AbstractCursor#fillWindow 方法,它填补了 CursorWindow 使用对象#的toString 。所以发生的是,字节数组的toString 方法被调用,它的存储的,而不是字节数组这是你想要的内容。解决这个问题的唯一办法,我可以看到的是,以实现自己的光标,这将填补 CursorWindow 适当的字节数组。

You won't be able to use a MatrixCursor to send the byte array. This is because it depends on AbstractCursor#fillWindow method which fills the CursorWindow using Object#toString. So what is happening is that the byte array toString method is being called and it's storing that instead of the contents of the byte array which is what you want. The only way around this I can see is to implement your own Cursor that will fill the CursorWindow appropriately for a byte array.

这篇关于通过内容提供商传递二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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