RemoteViews setImageViewBitmap () не работает - PullRequest
0 голосов
/ 30 мая 2018

Я занимаюсь разработкой простого приложения для Android с пользовательскими макетами уведомлений.Для этого я использую RemoteViews .У меня есть ImageView внутри моего макета, но я не могу установить для него растровое изображение.

Я использую этот код для установки растрового изображения:

layout.setImageViewBitmap(R.id.noteNotificationImage, bitmap)

Я также пытался использовать холст, но этомне не помогло:

val proxy = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
                    val c = Canvas(proxy)
                    c.drawBitmap(bitmap, Matrix(), null)
                    layout.setImageViewBitmap(R.id.noteNotificationImage, proxy)

Растровое изображение не равно нулю и все работает хорошо, когда я использую его в простых макетах, а не в RemoteViews.

Может кто-нибудь мне помочь?

1 Ответ

0 голосов
/ 30 мая 2018

Я не знаю, где проблема, но этот код прекрасно работает для меня

    @SuppressLint("NewApi")
    public void customNotification(String title, String description, String image, Bitmap bitmap) {


        Intent intent = new Intent(mContext,Activity.class);



        long when = System.currentTimeMillis();
        int icon = getNotificationIcon();

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.small_notification);
        RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.big_notification);
        Notification notification = new Notification(icon, getResources().getString(R.string.app_name), when);
        try {
            if (notification != null) {

                notification.contentView = simpleContentView;
                notification.contentIntent = pendingIntent;
                if (currentVersionSupportBigNotification()) {
                    notification.bigContentView = expandedView;
                }
                notification.contentView.setTextViewText(R.id.txt_title_notification, title);
                notification.contentView.setTextViewText(R.id.txt_desc_notification, description);
                if (image != null && !image.equals("")) {
                    try {
                        notification.contentView.setImageViewBitmap(R.id.img_poster_notification, bitmap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    notification.contentView.setImageViewResource(R.id.img_poster_notification, R.drawable.placeholder_144x214);
                }
                if (currentVersionSupportBigNotification()) {
                    notification.bigContentView.setTextViewText(R.id.txt_title_notification, title);
                    notification.bigContentView.setTextViewText(R.id.txt_desc_notification, description);

                    if (image != null && !image.equals("")) {
                        try {
                            notification.bigContentView.setImageViewBitmap(R.id.img_poster_notification, bitmap);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        notification.bigContentView.setImageViewResource(R.id.img_poster_notification, R.drawable.placeholder_144x214);
                    }
                }
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                notification.defaults |= Notification.DEFAULT_LIGHTS;
                notification.defaults |= Notification.DEFAULT_VIBRATE;//Vibration
                notification.defaults |= Notification.DEFAULT_SOUND;
                mNotifyManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
                mNotifyManager.notify(NOTIFICATION_ID, notification);
            }
        } catch (Exception e) {
            e.printStackTrace();
            GlobalApp.Log("Notification_exc", "" + e.getMessage());
        }
    }
...