我正在开发一个带有自定义通知布局的简单Android应用程序。为此,我使用 RemoteViews 。我的布局中有一个ImageView但我无法将位图设置为它。
我使用此代码设置位图:
layout.setImageViewBitmap(R.id.noteNotificationImage, bitmap)
我也试过使用画布,但这对我没有帮助:
val proxy = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
val c = Canvas(proxy)
c.drawBitmap(bitmap, Matrix(), null)
layout.setImageViewBitmap(R.id.noteNotificationImage, proxy)
当我在简单布局中使用它而不是在RemoteViews中时,Bitmap不是null并且一切正常。
任何人都可以帮助我吗?
答案 0 :(得分:0)
我不知道哪里有问题,但这段代码对我来说很好
@SuppressLint("NewApi")
public void customNotification(String title, String description, String image, Bitmap bitmap) {
Intent intent = new Intent(mContext,Activity.class);
long when = System.currentTimeMillis();
int icon = getNotificationIcon();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.small_notification);
RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.big_notification);
Notification notification = new Notification(icon, getResources().getString(R.string.app_name), when);
try {
if (notification != null) {
notification.contentView = simpleContentView;
notification.contentIntent = pendingIntent;
if (currentVersionSupportBigNotification()) {
notification.bigContentView = expandedView;
}
notification.contentView.setTextViewText(R.id.txt_title_notification, title);
notification.contentView.setTextViewText(R.id.txt_desc_notification, description);
if (image != null && !image.equals("")) {
try {
notification.contentView.setImageViewBitmap(R.id.img_poster_notification, bitmap);
} catch (Exception e) {
e.printStackTrace();
}
} else {
notification.contentView.setImageViewResource(R.id.img_poster_notification, R.drawable.placeholder_144x214);
}
if (currentVersionSupportBigNotification()) {
notification.bigContentView.setTextViewText(R.id.txt_title_notification, title);
notification.bigContentView.setTextViewText(R.id.txt_desc_notification, description);
if (image != null && !image.equals("")) {
try {
notification.bigContentView.setImageViewBitmap(R.id.img_poster_notification, bitmap);
} catch (Exception e) {
e.printStackTrace();
}
} else {
notification.bigContentView.setImageViewResource(R.id.img_poster_notification, R.drawable.placeholder_144x214);
}
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;//Vibration
notification.defaults |= Notification.DEFAULT_SOUND;
mNotifyManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
mNotifyManager.notify(NOTIFICATION_ID, notification);
}
} catch (Exception e) {
e.printStackTrace();
GlobalApp.Log("Notification_exc", "" + e.getMessage());
}
}