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