I've created an app (webview) radio, where I get artist and title from the page and show them in the notification. Now I wanted to insert the image of the song by taking the url from the html. I've already tried to do this, but when my app starts crashes!
I tried with a code found in another thread, but my app crashes
https://stackoverflow.com/a/16007659/11057248
public void displayNotification() {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setOngoing(true);
builder.setContentTitle(MainActivity.this.html);
builder.setContentText(MainActivity.this.html1);
builder.setLargeIcon(bitmap);
builder.setShowWhen(true);
builder.setOnlyAlertOnce(true);
builder.setAutoCancel(false);
builder.setContentIntent(resultPendingIntent);
builder.setPriority(Notification.PRIORITY_HIGH);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
Bitmap bitmap = getBitmapFromURL("https://URL");
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;
}
}
From what I understand and tested, this is what makes my app crash: HttpURLConnection connection = (HttpURLConnection) url.openConnection();