0

The following code shows how to load an image from my server into an ImageView with Picasso (on Android). I figured out that the image in the ImageView is changing when the method finishes - but I want to change it when the line of code is executing.

btn_startlive.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){


    while(something_that_is_true_for_a_long_time){        
       Picasso.with(img_live.getContext()).load("http://localhost:8000/livepicture")
                      .networkPolicy(NetworkPolicy.NO_CACHE)
                      .placeholder(img_live.getDrawable())
                      .memoryPolicy(MemoryPolicy.NO_CACHE)
                      .into(img_live);
         }      


            }
        });

In this example the image does not change. Is it because the method does not end? And how to fix it?

  • 2
    remove `SystemClock.sleep(2000);` – Pavneet_Singh Jan 16 '20 at 13:58
  • thats not the point, it was just to explain what my problem is. normally there is a loop around the picasso-line but the image never changes because the method does not come to an end – Alexander Z. Jan 16 '20 at 13:59
  • it's a network call(local host in this case) so will take time, use profiler to verify the network activities – Pavneet_Singh Jan 16 '20 at 14:00
  • Android only effectively picks up updates to views after your app's custom code finishes. Especially if your `onClick` runs on the same thread that updates UI. – M. Prokhorov Jan 16 '20 at 14:02
  • but if i edit the 2000 to a 20000, the image changes after 20 seconds. so the problem should be that the method does not come to an end – Alexander Z. Jan 16 '20 at 14:02
  • @M.Prokhorov what does this mean exactely? – Alexander Z. Jan 16 '20 at 14:03
  • @AlexanderZ, I just accidentally pressed "send" before I finished typing. – M. Prokhorov Jan 16 '20 at 14:04
  • @M.Prokhorov ah okay, thanks a lot. Do you know any kind of solution? – Alexander Z. Jan 16 '20 at 14:06
  • 1
    @AlexanderZ, I don't understand what kind of solution you expect. I assume that solution of removing `SystemClock.sleep` (which will indeed allow image to show up immediately) that was already suggested doesn't fit the bill. So if you update the question to better explain what you want from this code, I (or someone else for that matter) might provide more help. – M. Prokhorov Jan 16 '20 at 14:09
  • Ok, after the edit I see what the problem might be. Correct approach for this loop is along the lines of this question and its answers: https://stackoverflow.com/questions/34880389/how-to-loop-or-execute-a-function-every-5-seconds-on-android – M. Prokhorov Jan 16 '20 at 14:23
  • Or this one: https://stackoverflow.com/questions/9539416/update-the-ui-with-dynamic-text – M. Prokhorov Jan 16 '20 at 14:26
  • @M.Prokhorov Thanks a lot!!! The second answer of your first link seems to solved my problem! – Alexander Z. Jan 16 '20 at 14:35
  • @AlexanderZ, Ok. Then I'll mark your question as a duplicate of it, if you don't mind. – M. Prokhorov Jan 16 '20 at 15:38
  • Does this answer your question? [Update the UI with dynamic text](https://stackoverflow.com/questions/9539416/update-the-ui-with-dynamic-text) – M. Prokhorov Jan 16 '20 at 15:39
  • @M.Prokhorov don't know, but the timer fixed the problem – Alexander Z. Jan 17 '20 at 08:48
  • @AlexanderZ., my previous comment is just an automatic comment generated when I marked the question as a duplicate. – M. Prokhorov Jan 17 '20 at 15:54

1 Answers1

0

Your onClickListener() looks right. In your onClickListener() method SystemClock.sleep(2000); stops your application (All thread as I know). Because of that your image cannot load immediately.

Just remove SystemClock.sleep(2000); and that is it.

If you want to use your Picasso library more effeciently, you can check this answer

If your image is loading so slow after removing SystemClock.sleep() (you said that is just for showing your case), maybe your images are too large to process.

If you load a lot of image, try to use resize for better memory performance:

Picasso.with(getActivity()).load(getPixId).resize('widthImageView', 'heightImageView').noFade().into(imageView);

EDIT : If you try to load too large image files to Picasso, it shows images with some delay. For eliminate the problem that is about your image size or not, please add .resize() method in your Picasso line.

 Picasso.with(img_live.getContext())
.load("http://localhost:8000/livepicture") 
.networkPolicy(NetworkPolicy.NO_CACHE) 
.placeholder(img_live.getDrawable()) .memoryPolicy(MemoryPolicy.NO_CACHE)
.resize(100, 100) 
.into(img_live); // resizes the image to these dimensions (in pixel). does not respect aspect ratio 
TeachMeJava
  • 640
  • 1
  • 13
  • 35
  • Thanks. But if i delete `SystemClock.sleep(2000)` and put a while loop around the picasso-line, the image does not change too (because the method does not come to an end?). – Alexander Z. Jan 16 '20 at 14:08
  • @AlexanderZ, Your comment implies that your question doesn't currently hold a [mcve] of the problem you actually have. If so, please edit it and explain the problem better. – M. Prokhorov Jan 16 '20 at 14:11
  • @M.Prokhorov edited. – Alexander Z. Jan 16 '20 at 14:14
  • Can you add `.resize()` method in your Picasso line and get back to us? `Picasso.with(img_live.getContext()).load("http://localhost:8000/livepicture") .networkPolicy(NetworkPolicy.NO_CACHE) .placeholder(img_live.getDrawable()) .memoryPolicy(MemoryPolicy.NO_CACHE).resize(100, 100) .into(img_live);` // resizes the image to these dimensions (in pixel). does not respect aspect ratio @AlexanderZ. – TeachMeJava Jan 16 '20 at 14:30
  • I want to eliminate if your problem is about image size or not. @AlexanderZ. – TeachMeJava Jan 16 '20 at 14:32
  • Thanks @TeachMeJava! I think my problem is solved but I think your guess will improve my project. – Alexander Z. Jan 16 '20 at 14:38
  • Thank you too @AlexanderZ. I updated my answer. Just accept my answer and it will be usefull for other people. – TeachMeJava Jan 16 '20 at 14:48