该语句将使用一种方法将 URL(自然是指向图像的 URL)转换为Bitmap
.
Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/YOUR_USER_ID/picture?type=large");
Note:既然您提到了 Facebook 个人资料,我已经添加了一个 URL,可以让您获取 Facebook 用户的大尺寸个人资料图片。但是,您可以将其更改为指向您需要在Notification
.
以及从您在上面的语句中指定的 URL 获取图像的方法:
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();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
现在通过bitmap
上面创建的实例Notification.Builder
实例。我称之为builder
在此示例代码中。它用在这一行中:builder.setLargeIcon(bitmap);
。我假设你知道如何显示实际的Notification
以及它的配置。所以我将跳过该部分并仅添加builder.
// CONSTRUCT THE NOTIFICATION DETAILS
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Some Title");
builder.setContentText("Some Content Text");
builder.setLargeIcon(bitmap);
builder.setContentIntent(pendingIntent);
哦,差点忘了,如果您还没有这样做,您将需要在清单中设置此权限:
<uses-permission android:name="android.permission.INTERNET" />