【问题标题】:Load Image With Picasso to a bitmap first首先将带有毕加索的图像加载到位图
【发布时间】:2016-07-23 12:43:46
【问题描述】:

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

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

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

【问题讨论】:

    标签: android bitmap picasso


    【解决方案1】:

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

    【讨论】:

    • YESSSSSS,它正在工作。你能告诉我更多关于为什么用毕加索制作实例如此重要吗?
    • 如果您不将Target 设置为实例字段,Target 将被垃圾回收。更多信息,请看本期github.com/square/picasso/issues/352
    【解决方案2】:

    你可以这样做

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

    【讨论】:

    • 看兄弟,如果我想保存位图,这是可行的。我不想保存它。我只想使用该位图并将其保存到另一个位图,因为我想对其进行图像效果。
    • 你要缓存图片以后再用吗??
    • 不,我只是想把它放在一些位图中,这样我就可以将它发送到另一个使用渲染脚本的函数。我想要这个:onBitmapLoaded.... otherBitmap = bitmap; imageView.setImageBitmap(位图);但它工作不正常。试试看你会看到的。
    • 好的,那么你可以从 onBitmapLoaded 函数中得到它有什么问题
    • 将位图设置为 imageView 时出现问题。试试看,它不能正常工作。我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2014-09-26
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多