Home » Android » java – Passing binary blob through a content provider

java – Passing binary blob through a content provider

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 [[email protected], 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.

How to&Answers:

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.

Answer:

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.

Answer:

You need to override fillWindow in your AbstractCursor implementation. Here’s one that works on a content provider for JUST BLOBS