Im new for android, I want to send notification with image.If i use image from drawable folder means i can do. But, i want to pull image from url and then send to it... I tried some code its crash my app. How to do Anyone guide to me!
My code here:
protected static void postNotification(Intent intentAction, Context context,String msg,String url){
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL);
Bitmap bitmap = new ImageDownloaderTask().doInBackground(url);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.tapn)
.setContentTitle("Notification")
.setContentText(msg)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.getNotification();
mNotificationManager.notify(R.string.notification_number, notification);
}
ImageDownloaderTask.java:
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private Exception exception;
@Override
public Bitmap doInBackground(String... params) {
return downloadBitmap(params[0]);
}
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
Log.d("URLCONNECTIONERROR", e.toString());
if (urlConnection != null) {
urlConnection.disconnect();
}
Log.w("ImageDownloader", "Error downloading image from " + url);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
protected void onPostExecute(Bitmap feed) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
My logcat:
D/URLCONNECTIONERROR: android.os.NetworkOnMainThreadException
W/ImageDownloader: Error downloading image from https://www.google.com/intl/en_ALL/images/logo.gif
Thanks in advance!