使用 volley 将图像资源设置为 remoteView
Set image resource to remoteView using volley
我从 android 中的服务播放音乐流,所以当音乐开始播放时,上方会出现一条通知并显示 play/pause 按钮、音乐标题和音乐缩略图封面照片 。我在服务代码中启动通知,如下所示:
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
notification = new Notification(icon, "", when);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationContentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
notificationContentView.setImageViewResource(R.id.thumbnailNotification, R.drawable.ic_launcher);
notificationContentView.setTextViewText(R.id.albumtitleNotification, sntSongAlbum);
notificationContentView.setTextViewText(R.id.songnameNotification, sntSongTitle);
notification.contentView = notificationContentView;
Intent notificationIntent = new Intent(this, OpenSongActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
其中 custom_notification.xml 看起来像 :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/thumbnailNotification"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/albumtitleNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/thumbnailNotification"
android:layout_marginRight="8dp"
android:paddingTop="5dp"
style="Custom Notification Title" />
<TextView android:id="@+id/songnameNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/thumbnailNotification"
android:layout_below="@id/albumtitleNotification"
android:layout_marginRight="8dp"
style="Custom Notification Text" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="8dp"
android:id="@+id/closeButton"
android:background="@android:drawable/presence_offline"
android:clickable="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playNotification"
android:layout_marginLeft="8dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="@android:drawable/ic_media_pause" />
这完美地工作并设置了包含在 ImageView 资源文件夹中的 ic_launcher 图标,但我想要实现的是将此图像设置为来自 url 的图像,因为我使用 volley 库下载和设置图像我用 thumbnailNotification.xml 中的 id=thumbnailNotification 替换了 ImageView 到以下内容:
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnailNotification"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
aandroid:layout_marginRight="10dp />
但是现在如何从服务中设置它?
我最终使用 BitmapFactory 来设置图像,因为我没有找到任何方法来使用 volley 和 remoteViews :
public class NotificationImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override protected Bitmap doInBackground(Void... params) {
Bitmap bm = null;
try {
URL aURL = new URL(params[0]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
@Override protected void onPostExecute(Bitmap result) {
notificationContentView.setImageViewBitmap(R.id.thumbnailNotification , result);
}
}
...
new NotificationImageAsyncTask().execute("Image URL");
我从 android 中的服务播放音乐流,所以当音乐开始播放时,上方会出现一条通知并显示 play/pause 按钮、音乐标题和音乐缩略图封面照片 。我在服务代码中启动通知,如下所示:
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
notification = new Notification(icon, "", when);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationContentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
notificationContentView.setImageViewResource(R.id.thumbnailNotification, R.drawable.ic_launcher);
notificationContentView.setTextViewText(R.id.albumtitleNotification, sntSongAlbum);
notificationContentView.setTextViewText(R.id.songnameNotification, sntSongTitle);
notification.contentView = notificationContentView;
Intent notificationIntent = new Intent(this, OpenSongActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
其中 custom_notification.xml 看起来像 :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/thumbnailNotification"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/albumtitleNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/thumbnailNotification"
android:layout_marginRight="8dp"
android:paddingTop="5dp"
style="Custom Notification Title" />
<TextView android:id="@+id/songnameNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/thumbnailNotification"
android:layout_below="@id/albumtitleNotification"
android:layout_marginRight="8dp"
style="Custom Notification Text" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="8dp"
android:id="@+id/closeButton"
android:background="@android:drawable/presence_offline"
android:clickable="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playNotification"
android:layout_marginLeft="8dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="@android:drawable/ic_media_pause" />
这完美地工作并设置了包含在 ImageView 资源文件夹中的 ic_launcher 图标,但我想要实现的是将此图像设置为来自 url 的图像,因为我使用 volley 库下载和设置图像我用 thumbnailNotification.xml 中的 id=thumbnailNotification 替换了 ImageView 到以下内容:
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnailNotification"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
aandroid:layout_marginRight="10dp />
但是现在如何从服务中设置它?
我最终使用 BitmapFactory 来设置图像,因为我没有找到任何方法来使用 volley 和 remoteViews :
public class NotificationImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override protected Bitmap doInBackground(Void... params) {
Bitmap bm = null;
try {
URL aURL = new URL(params[0]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
@Override protected void onPostExecute(Bitmap result) {
notificationContentView.setImageViewBitmap(R.id.thumbnailNotification , result);
}
}
...
new NotificationImageAsyncTask().execute("Image URL");