0

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();

Error: https://hastebin.com/golifodaci.cs

Mirisna
  • 1
  • 1
  • 3
    Welcome to StackOverflow! This is way to much code for quick help. Please try to create a [mcve] and [edit] your question. – Hille Feb 13 '19 at 15:25
  • What does `makes my app chash` mean? What error? – Limmy Feb 13 '19 at 15:25
  • 1
    Hi @Limmy this is my error https://hastebin.com/golifodaci.cs – Mirisna Feb 13 '19 at 15:29
  • 1
    Possible duplicate of [How do I fix android.os.NetworkOnMainThreadException?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) – Jozef Chocholacek Feb 13 '19 at 15:32

0 Answers0