通过内容提供程序传递二进制 blob
我有一个针对我的一组 Android 应用程序定制的内容提供程序,它需要公开的内容之一是一个小型(20-30 KiB)字节数组。这些 blob 的 URI 如下所示:
content://my.authority/blob/#
其中 #
是行号;生成的游标具有标准的 _id
列和数据列。我在提供程序的 query()
方法中使用 MatrixCursor
:
byte[] byteData = getMyByteData();
MatrixCursor mc = new MatrixCursor(COLUMNS);
mc.addRow(new Object[] { id, byteData });
稍后,在使用数据的应用程序中,我这样做:
Cursor c = managedQuery(uri, null, null, null, null);
c.moveToFirst();
byte[] data = c.getBlob(c.getColumnIndexOrThrow("data"));
但是,数据不包含我的内容原始字节数组;相反,它包含类似 [B@435cc518
的内容,它看起来更像是数组的地址而不是内容。我尝试将字节数组包装在 java.sql.Blob 的实现中,认为它可能正在寻找该字节数组,因为内容提供程序子系统被编写为易于与 SQLite 一起使用,但事实并非如此。没有帮助。
有人让它发挥作用吗?如果数据位于文件系统中,我可以使用 ContentProvider
中的方法向客户端提供编组的 InputStream
,但我尝试发送的数据back 作为内容提供商的 APK 中的资源存在。
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/#
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"));
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Ice Cream Sandwich 源获取 MatrixCursor。它正确实现了
getBlob
。下面是修改后的源代码,如果你想使用它。您可以在 Android 1.6+ 项目中使用它。
Get the MatrixCursor from Ice Cream Sandwich sources. It implements
getBlob
correctly.Modified source code is below, if you want to use it. You can use it in Android 1.6+ projects.
您将无法使用
MatrixCursor
发送字节数组。这是因为它依赖于AbstractCursor#fillWindow
方法,该方法使用Object#toString
填充CursorWindow
。因此,正在发生的事情是调用字节数组 toString 方法,并且它存储该方法,而不是您想要的字节数组的内容。我能看到的解决这个问题的唯一方法是实现您自己的游标,它将为字节数组适当地填充CursorWindow
。You won't be able to use a
MatrixCursor
to send the byte array. This is because it depends onAbstractCursor#fillWindow
method which fills theCursorWindow
usingObject#toString
. So what is happening is that the byte arraytoString
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 theCursorWindow
appropriately for a byte array.您需要在 AbstractCursor 实现中重写 fillWindow。这是适用于 JUST BLOBS 的内容提供商的一个
You need to override fillWindow in your AbstractCursor implementation. Here's one that works on a content provider for JUST BLOBS