如何使用Picasso单击按钮将图像保存到存储器?

时间:2018-12-14 08:54:08

标签: java android image picasso

我真的很喜欢Java和android开发,并且我正在尝试制作一个简单的应用程序,该程序将显示随机图像并下载您喜欢的图像。 我已经实现了显示部分并添加了下载按钮,但是我不知道如何使用毕加索的目标功能下载图像。尝试过在线搜索,但找不到最新的解释。如果有人能做到这一点,我将非常感激。

到目前为止,我的代码如下:

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

            }}



        );
    }
}

0 个答案:

没有答案