Storing a Bitmap into a ContentProvider from a BroadcastReceiver
I'm working on an app that, upon instillation of certain apps will send a message to them to register with a content provider. It all works fine, the strings are stored, but I开发者_StackOverflow中文版 can't get the byte[] blob (a bitmap .png file) to convert properly from within onReceive()
in the broadcastreceiver.
@Override
public void onReceive(Context context, Intent intent) {
Log.v("xxxxxxxxxxxxx", "onReceive, from within RegisterApp");
this.context = context;
ContentValues values = new ContentValues();
values.put("app_name", getAppName());
values.put("app_description", getAppDescription());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap b = getAppIcon();
b.compress(Bitmap.CompressFormat.PNG, 0, stream);
byte[] blob = stream.toByteArray();
values.put("icon", blob);
context.getContentResolver().insert(
Uri.parse("content://blahblahblahblah"), values);
}
I hijack the context so that I can get a hold on the image from the getAppIcon() call (You extend this class which is an abstract class extending BroadcastReceiver)
i.e.
@Override
protected Bitmap getAppIcon() {
Log.v(TAG, "putting icon");
return BitmapFactory.decodeResource(this.context.getResources(), R.drawable.icon);
}
But my understanding of the context is limited and I think this is what is causing it to be stored incorrectly.
I've also tried getting the bitmap from the resources when onReceive()
is called and that doesn't work either.
I'm simply an idiot. Nothing is wrong with storing the image in this fashion (provided it is a .png), I simply forgot to setContentView().
Sorry!
精彩评论