大家好我想在我的应用程序中实现通知。问题是我希望每个通知都有可能从imageview获取某个图像。
由于documentation表示setSmallIcon()
方法只能使用int resId
作为参数,因此我必须使用setLargeIcon()
方法。如何将来自URL的图像转换为位图?
已经尝试过:
Bitmap bmp = BitmapFactory.decodeFile(getIntent().getStringExtra("stockImage"));
builder.setLargeIcon(bmp);
它给了我错误:
02-15 11:34:34.576 1615-1615/com.kostas.stockpredictions E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: http:/www.chatapp.info/myProject/images/ALPHA.png: open failed: ENOENT (No such file or directory)
我使用Ion库将此url设置为Imageview:
iv = (ImageView)findViewById(R.id.currentStockImageViewItem);
Ion.with(iv).placeholder(R.drawable.ic_chat).error(R.drawable.ic_chat).load(i.getStringExtra("stockImage"));
修改
class GetBitmapUrl extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
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;
}
}
@Override
protected void onPostExecute(Bitmap result) {
builder.setLargeIcon(result);
}
}
public void getBitmap(){
GetBitmapUrl task = new GetBitmapUrl();
task.execute(getIntent().getStringExtra("stockImage"));
}
我在这里称这个方法:
Button notify = (Button)findViewById(R.id.buttonNotify);
notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder = new NotificationCompat.Builder(StockItem.this);
builder.setContentTitle(name);
builder.setContentText(getString(R.string.notifyText) + " " + new DecimalFormat("###.##").format(avg));
builder.setWhen(System.currentTimeMillis());
getBitmap();
//builder.setSmallIcon(R.drawable.ic_launcher_stock_custom_icon);
builder.setTicker(getString(R.string.notifyTicker));
builder.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/carme"));
builder.setDefaults(NotificationCompat.DEFAULT_LIGHTS | NotificationCompat.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
Intent intent = new Intent(StockItem.this, ListLoaderActivity.class);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(StockItem.this);
taskStackBuilder.addNextIntent(intent);
taskStackBuilder.addParentStack(ListLoaderActivity.class);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
});
现在通知没有显示..
是否可以在imageview中使用带有图像的位图?
提前致谢!!!
答案 0 :(得分:2)
使用此代码
Notification notification = new Notification(R.drawable.images, getText(R.string.time),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ActivityStart.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "The Service has started",
"This is a foreground service", pendingIntent);
startForeground(3, notification);
要下载图像,请使用AsyncTask的doInBackgroundMethod中的以下代码。
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;
}
}
现在覆盖Asynctask中的onPostExecute方法,如下所示:
// change the return type of doInBackGround to InputStream
@Override
public void onPostExecute(InputStream in){
// let the bitmap in the activity be activityBitmap
Bitmap activityBitmap = BitmapFactory.decodeStream(input);
}
一个例子是
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
答案 1 :(得分:2)
自定义通知布局
通知框架允许您定义自定义 通知布局,定义通知的外观 一个RemoteViews对象。自定义布局通知类似于 正常通知,但它们基于中定义的RemoteView XML布局文件。
自定义通知布局的可用高度取决于 通知视图。普通视图布局限制为64 dp,和 展开的视图布局限制为256 dp。
要定义自定义通知布局,请先实例化a 用于扩展XML布局文件的RemoteViews对象。然后,相反 调用setContentTitle()等方法,调用setContent()。至 在自定义通知中设置内容详细信息,请使用方法 RemoteViews用于设置视图子项的值:
在单独的文件中为通知创建XML布局。您 可以使用您希望的任何文件名,但您必须使用扩展名.xml 在您的应用中,使用RemoteViews方法来定义通知 图标和文字。将此RemoteViews对象放入您的 NotificationCompat.Builder通过调用 setContent()。避免设置 background在您的RemoteViews对象上可绘制,因为您的文本 颜色可能变得不可读。
有关详细信息,请转到 http://developer.android.com/guide/topics/ui/notifiers/notifications.html