Search code examples
androidpicasso

Android - how to load image without storing / caching with picasso


I use firebase api to send notification. I handle title and image Url keys.

I use image url key to load image with picasso into imageview. My problem is that " I sent a notification but i did not click it, after first notification i send second one with different image url. I clicked second notification. App starts and I see the picture in imageview in first address.

I mean first notification image url is : www.....com/11.jpg second one image url is : www.....com/22.jpg

So, when i clicked last notification i should see www.....com/22.jpg instead of www.....com/11.jpg .

Intent intent = getIntent();

    String url = intent.getStringExtra("imgUrl");
    String catTitle = intent.getStringExtra("catTitle");

    imageView = (ImageView) findViewById(R.id.thumbnail);

    Picasso.with(this).load(url)
            .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .resize(300,250)
            .into(imageView);

And also i debugged app and url is right but The picture inside imageview is not that picture. Show a way to solve this.

 String message = remoteMessage.getData().get("message");
    //imageUri will contain URL of the image to be displayed with Notification
    String imageUri = remoteMessage.getData().get("image");
    String catTitle = remoteMessage.getData().get("catTitle");

    //To get a Bitmap image from the URL received
    bitmap = getBitmapfromUrl(imageUri);

    sendNotification(message, imageUri, bitmap,catTitle/*,warn,TrueOrFlase*/);

}

This is my metot.

private void sendNotification(String messageBody, String imgUrl, Bitmap image, String catTitle/*,String warn, String TrueOrFalse*/) {
    Intent intent = new Intent(this, AnotherActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //intent.putExtra("AnotherActivity", TrueOrFalse);
    intent.putExtra("imgUrl", imgUrl);
    intent.putExtra("catTitle", catTitle);
    //intent.putExtra("bitmap",bitmap);
    //intent.putExtra("warn", warn);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.drawable.ic_stat_onesignal_default)
            .setContentTitle(messageBody)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 , notificationBuilder.build());
}

Solution

  • Try using NO_STORE policy:

    Picasso.with(this).load(url)
                     .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .into(imageView);
    

    NO_CACHE

    Skips memory cache lookup when processing a request.

    NO_STORE

    Skips storing the final result into memory cache. Useful for one-off requests to avoid evicting other bitmaps from the cache.

    UPDATE

    You have to use different IDs for your notification to avoid the replace :

    notificationManager.notify(0 , notificationBuilder.build());
    

    If you don't have an ID, you could use a random number:

     int  randomNum = 0 + rand.nextInt((999 - 0) + 1);
            int notificationID = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE) + randomNum;
    notificationManager.notify(notificationID, notificationBuilder.build());