首先使用Picasso将图像加载到位图

时间:2016-07-23 12:43:47

标签: android bitmap picasso

我正在使用毕加索。 我想先将图像添加到位图,然后将其添加到imageview。我正在使用以下代码行,使用uri从库中添加图像并在图像视图中显示它。我想先将它保存在位图上。我该怎么办:

Picasso.with(this).load(uriadress).into(imageView);

但我想先将它保存在位图上。

2 个答案:

答案 0 :(得分:10)

Picasso持有弱Target实例。
因此,最好将Target作为实例字段 见:https://stackoverflow.com/a/29274669/5183999

private Target mTarget;

void loadImage(Context context, String url) {

    final ImageView imageView = (ImageView) findViewById(R.id.image);

    mTarget = new Target() {
        @Override
        public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
            //Do something
            ...

            imageView.setImageBitmap(bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    Picasso.with(context)
            .load(url)
            .into(mTarget);
}

答案 1 :(得分:3)

你可以这样做

private Target image;
image = new Target() {
        @Override
        public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + FILEPATH);
                    try {
                        file.createNewFile();
                        FileOutputStream outstream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 75, outstream);
                        outstream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
Picasso.with(this)
        .load(currentUrl)
        .into(image);