Hi guys i am trying to implement notifications into my application. The thing is that i want each notification to have a certain image taken from the imageview if possible.
Since the documentation says that the setSmallIcon()
method can only take an int resId
as parameter i have to use the setLargeIcon()
method. How can i convert an Image that comes from URL into Bitmap?
Already Tried:
Bitmap bmp = BitmapFactory.decodeFile(getIntent().getStringExtra("stockImage"));
builder.setLargeIcon(bmp);
It gives me this error:
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)
I set this url into an Imageview using Ion library like this:
iv = (ImageView)findViewById(R.id.currentStockImageViewItem);
Ion.with(iv).placeholder(R.drawable.ic_chat).error(R.drawable.ic_chat).load(i.getStringExtra("stockImage"));
EDIT:
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"));
}
And i call this method here:
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());
}
});
Now the notification does not show up..
Is it possible to have a Bitmap with the image from imageview?
Thanks in advance!!!