I am trying to load https://api.androidhive.info/images/minion.jpg
into a bitmap in Android. I have used AsyncTask to perform download in background and in onPostExecute I am generating a notification. The notifications is displayed but without the image.
The problem is that the Bitmap returned is null. There seems to be no error in Network calls as input stream and connections are not null.
Methods in NotificationUtils:
public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.mipmap.ic_launcher;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
if (!TextUtils.isEmpty(imageUrl)) {
if (imageUrl != null && imageUrl.length() > 4 && Patterns.WEB_URL.matcher(imageUrl).matches()) {
/*ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.i("ayusch",loadedImage.toString());
}
});*/
/*Bitmap test = null;
try {
test = Picasso.with(mContext).load(imageUrl).get();
} catch (IOException e) {
e.printStackTrace();
}*/
//Bitmap bitmap = getBitmapFromURL(imageUrl);
/*ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.i("ayusch",loadedImage.toString());
}
});*/
//Bitmap bitmap1 = imageLoader.loadImageSync(imageUrl);
//Bitmap mbitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.verifiedicon);
new AsyncTask<String, String, Bitmap>() {
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(input);
input.close();
return bmp;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
showBigNotification(bitmap, mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
} else {
showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
Toast.makeText(mContext, "Bitmap is null", Toast.LENGTH_SHORT).show();
}
}
}.execute(imageUrl);
}
} else {
showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
playNotificationSound();
}
}
Creating notification :
private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_ID_BIG_IMAGE, notification);
}
I have already tried UniversalImageLoader as :
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.i("ayusch",loadedImage.toString());
}
});
When I use this , the notification is not at all shown.
I have tried using picasso as :
Bitmap test = Picasso.with(mContext).load(imageUrl).get();
But again the notification is not shown !!
Please help