我创建了一个应用程序(网络视图)广播,从页面中获取艺术家和标题,并在通知中显示它们。现在,我想通过从html获取网址来插入歌曲的图像。我已经尝试过执行此操作,但是当我的应用启动时会崩溃!
我尝试了在另一个线程中找到的代码,但是我的应用程序崩溃了
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;
}
}
根据我的理解和测试,这是导致我的应用崩溃的原因: HttpURLConnection connection =(HttpURLConnection)url.openConnection();