我想在通知栏中显示大尺寸图像,因为我正在使用此代码
我的图像来自服务器。所以在 AsyncTask 内将其转换为位图并在 Activity ImageView 中显示其工作正常,但是当使用通知生成器在通知中加载相同的位图时显示错误
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
imgView.setImageBitmap(img);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(img))
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
01-19 12:28:32.556 1737-1737/? W/ProgressBarDelegate: Unknown Drawable subclass, src=android.graphics.drawable.ScaleDrawable@3c75db5
01-19 12:28:32.590 1737-1737/? E/StatusBar: couldn't inflate view for notification com.synergy.fcmdemo/0x0
android.widget.RemoteViews$ActionException: view: android.widget.FrameLayout doesn't have method: setEmphasizedMode(boolean)
android.widget.RemoteViews.getMethod(RemoteViews.java:851)
android.widget.RemoteViews.-wrap5(RemoteViews.java)
android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1411)
android.widget.RemoteViews.performApply(RemoteViews.java:3425)
android.widget.RemoteViews.apply(RemoteViews.java:3160)
com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:615)
com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1036)
at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1015)
at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:2088)
at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:472)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6205)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
01-19 12:28:32.591 1737-1737/? E/StatusBar: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.synergy.fcmdemo user=UserHandle{0} id=0 tag=null key=0|com.synergy.fcmdemo|0|null|10137: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE))
01-19 12:28:32.594 1737-1737/? D/StatusBar: onNotificationRemoved: Key: 0|com.synergy.fcmdemo|0|null|10137
最佳答案
试试他的,为我工作
使用Thread从服务器获取图像
new Thread(new Runnable() {
@Override
public void run() {
try {
showBigNotification();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
发送通知
private void showBigNotification() {
Bitmap bitmap = getBitmapFromURL(**Your image url**);
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.setBigContentTitle("title");
bigPictureStyle.setSummaryText("message");
bigPictureStyle.bigPicture(bitmap);
Intent notificationIntent = new Intent(mContext, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext,
101, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = mContext.getResources();
Notification.Builder builder = new Notification.Builder(mContext);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setTicker(res.getString(R.string.app_name))
.setStyle(bigPictureStyle)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Title")
.setContentText("Message");
Notification n = builder.build();
if (nm != null) {
nm.notify(1001, n);
}
}
从服务器获取图像
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
关于java - 新的NotificationCompat.BigPictureStyle()显示错误无法膨胀通知 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48335849/