0

I'm really to Java and android development and I'm trying to make a simple app which would display random images and download the one that you like. I have implemented the display part and added the download button, but I'm not able to figure out how to download the image using the target function for Picasso. Tried searching online but couldn't find any up to date explanation. I would be really grateful if someone could ELI5 that.

The code I have so far is as follows:

public class MainActivity extends AppCompatActivity {
    ImageView imageView ;
    Button button ;
    Button button2;
    String[] arr = {"https://i.redd.it/pyge7os0xr321.jpg", "https://i.redd.it/lzm6coywrp321.jpg",
                    "https://i.redd.it/l85mmi9c6p321.jpg","https://i.redd.it/vm25lg6umn321.jpg","https://i.redd.it/ca7o63wzpn321.jpg"};


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.imageView);
        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Picasso.get()
                        .load(arr[ThreadLocalRandom.current().nextInt(0, 4 + 1)])
                        .error(R.mipmap.ic_launcher)
                        .resize(300,300)
                        .into(imageView);

            }}




        );

        button2 = (Button)findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                String imageUrl = arr[ThreadLocalRandom.current().nextInt(0, 4 + 1)];

                Picasso.get()
                        .load(arr[ThreadLocalRandom.current().nextInt(0, 4 + 1)])
                        .error(R.mipmap.ic_launcher)
                        .resize(300,300)
                        .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        // Bitmap is loaded, use image here
                        imageView.setImageBitmap(bitmap);
                    }
                    public void onBitmapFailed() {
                        // Fires if bitmap couldn't be loaded.
                    }
                });

            }}



        );
    }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Vatsal Mishra
  • 109
  • 2
  • 10
  • 2
    Head over to [here](https://stackoverflow.com/questions/32799353/saving-image-from-url-using-picasso) – Tura Dec 14 '18 at 09:05
  • I've already tried this by putting the target part in the ".into", but it did not work. – Vatsal Mishra Dec 14 '18 at 09:52
  • Did not work how? Maybe you should check the permission like WRITE_EXTERNAL_STORAGE?? – Tura Dec 14 '18 at 09:55
  • I've added permissions and I'm getting these 2 errors: 1.error: is not abstract and does not override abstract method onBitmapFailed(Exception,Drawable) in Target 2. error: method does not override or implement a method from a supertype – Vatsal Mishra Dec 14 '18 at 10:23
  • @Vastal Mishra You should override methods from supertype (Interface or Abstract class). If you're using Android Studio, press [Ctrl + I] to implement missing methods. – Tura Dec 14 '18 at 10:53

0 Answers0