gpt4 book ai didi

java - 大图标位图在通知中显示为白色方 block ?

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:36 25 4
gpt4 key购买 nike

我在从通知中使用的 URL 生成 Bitmap 时遇到了这个问题。但是,在我的手机上,Bitmap 显示为一个白色的小方 block 。我调查了一下,发现很多帖子都在谈论它:Icon not displaying in notification: white square shown instead

而且我确信我的通知的 Small Icon 确实是透明的。但是,对于 Large Icon,我意识到 Large Icon 不能透明,因为它实际上是我从 URL 生成的 Bitmap。我怎样才能解决这个问题并确保图像正确呈现而不是让 Large Icon 显示为白色方 block ?这是我的尝试:

通知服务.java:

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setContentTitle(getString(R.string.app_name))
.setContentText(remoteMessage.getNotification().getBody())
.setTicker(remoteMessage.getFrom() + " has responded!")
.setLargeIcon(AndroidUtils.getBitmapFromURL(remoteMessage.getNotification().getIcon()))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setSmallIcon(R.drawable.ic_tabs_notification_transparent);

AndroidUtils.java:

public static Bitmap getBitmapFromURL(String userId) {
try {
URL imgUrl = new URL("https://graph.facebook.com/" + userId + "/picture?type=large");
InputStream in = (InputStream) imgUrl.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
Bitmap output;
Rect srcRect;
if (bitmap.getWidth() > bitmap.getHeight()) {
output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
srcRect = new Rect((bitmap.getWidth()-bitmap.getHeight())/2, 0, bitmap.getWidth()+(bitmap.getWidth()-bitmap.getHeight())/2, bitmap.getHeight());
} else {
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
srcRect = new Rect(0, (bitmap.getHeight()-bitmap.getWidth())/2, bitmap.getWidth(), bitmap.getHeight()+(bitmap.getHeight()-bitmap.getWidth())/2);
}

Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

float r;

if (bitmap.getWidth() > bitmap.getHeight()) {
r = bitmap.getHeight() / 2;
} else {
r = bitmap.getWidth() / 2;
}

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, srcRect, rect, paint);
return output;
} catch (IOException e) {
FirebaseCrash.report(e);
return null;
}

显示我的问题的图像:

enter image description here

编辑:Build.gradle 文件显示:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
applicationId '<ID>'
multiDexEnabled true
minSdkVersion 21
targetSdkVersion 23
versionCode 12
versionName ".12"
signingConfig signingConfigs.Tabs
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
zipAlignEnabled true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.Tabs
}
debug {
applicationIdSuffix ".debug"
debuggable true
minifyEnabled false
signingConfig signingConfigs.Tabs
}
}
//DatabaseReference stuff
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
dexOptions {
javaMaxHeapSize "4g"
}
productFlavors {
}
}

最佳答案

try {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(getNotificationIcon());
Bitmap icon = BitmapFactory.decodeResource(CXGcmListenerService.this.getResources(), R.drawable.ic_launcher);
mBuilder.setLargeIcon(icon);
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentText(msg);
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg));
mBuilder.setContentTitle(getString(R.string.app_name));
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setAutoCancel(true);
mBuilder.setSound(soundUri); //This sets the sound to play
Intent intent = new Intent(CXGcmListenerService.this, CXMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notificationIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(notificationIntent);

NotificationManager notifyManager = (NotificationManager) CXGcmListenerService.this.getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION_ID++;
notifyManager.notify("" + System.currentTimeMillis(), NOTIFICATION_ID, mBuilder.build());
} catch (Resources.NotFoundException e) {
CXLog.e(TAG, "" + e.getLocalizedMessage(), e);
}

请尝试获取通知中的图标。

关于java - 大图标位图在通知中显示为白色方 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com