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.
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.