Derek Toh
Derek Toh

Reputation: 33

Not receiving value from Thread

After the thread is completed, I want to retrieve the value of bitmap to be placed in the notification, but I got NullPointerException at Log.d("Bitmap1",bitmap.toString()); . Why is the bitmap value null?

@Override
public void onReceive(final Context context, Intent intent) {
  Iterator itr = json.keys();
    while (itr.hasNext()) {
        String key = (String) itr.next();

        Log.d(TAG, "..."+key+ "=>" +json.getString(key));
        if (key.equals("customdata"))
        {
            msg=json.getString(key);    
            Log.d(TAG,msg.toString());
        }
        if(key.equals("image_url"))
        {
            msg1=json.getString(key);       
            Log.d("msg1",msg1.toString());
        }
        if(key.equals("alert"))
        {
            msg2=json.getString(key);
            Log.d(TAG,msg2.toString());
        }
        if(key.equals("title"))
        {
            msg3=json.getString(key);
            Log.d(TAG,msg3.toString());
        }
    }
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
        try {
             bitmap=getBitmapFromURL(msg1);
             Log.d("Bitmap",bitmap.toString());

        } catch (Exception e) {
             e.printStackTrace();
        }
        }
    });
    thread.start();

    Log.d("Bitmap1",bitmap.toString());
    Notification noti = new NotificationCompat.Builder(context)
    .setContentTitle(msg3)
    .setContentText(msg2)
    .setContentIntent(pl)
    .setLargeIcon(bitmap)
    .setAutoCancel(true)
    .build();

    NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(50, noti);


    }
    } catch (JSONException e) {
        Log.d(TAG, "JSONException: " + e.getMessage());
    }        

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;
    }
}   

Upvotes: 0

Views: 39

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Create custom interface for get bitmap :

interface CallBackListener{
  public void onCallComplete(Bitmap bitmap);
}

Return bitmap using interface :

public Void getBitmapFromURL(String strURL,CallBackListener callBackListener) {
  Bitmap myBitmap;
  try {
       URL url = new URL(strURL);
       HttpURLConnection connection = (HttpURLConnection) url.openConnection();
       connection.setDoInput(true);
       connection.connect();
       InputStream input = connection.getInputStream();
       myBitmap = BitmapFactory.decodeStream(input);
   } catch (IOException e) {
       e.printStackTrace();
   }
   callBackListener.onCallComplete(myBitmap);
}

Implement interface :

Thread thread = new Thread(new Runnable(){
   @Override
   public void run() {
      try {
          bitmap=getBitmapFromURL(msg1,new CallBackListener() {
          @Override
          public void onCallComplete(Bitmap bitmap) {
               Log.d("Bitmap1",bitmap.toString());
               Notification noti = new NotificationCompat.Builder(context)
               .setContentTitle(msg3)
               .setContentText(msg2)
               .setContentIntent(pl)
               .setLargeIcon(bitmap)
               .setAutoCancel(true)
               .build();
               NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
               nm.notify(50, noti);
          }
        });
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
});

thread.start();

Upvotes: 1

Related Questions