Android:不调用Picasso onBitMapLoaded加载图像 [英] Android : Load image with Picasso onBitMapLoaded not called

查看:221
本文介绍了Android:不调用Picasso onBitMapLoaded加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中分享Instagram图片.我有图片的网址,并使用Picasso库下载图片.

I'm trying to share image on Instagram in my app. I've url of the image and using Picasso library to download image.

Target target = new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.d(TAG, "Bitmap Loaded");
                File outputDir = getApplicationContext().getCacheDir(); // context being the Activity pointer
                try {
                    File outputFile = File.createTempFile("instagram", "png", outputDir);
                    outputFile.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(outputFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                    ostream.close();
                    Log.d(TAG, "Image downloaded");
                    shareOnInstagram(Uri.fromFile(outputFile));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };
        Picasso.with(this).load(imageUrl).into(target);

但是永远不会调用onBitmapLoaded.还有其他方法可以通过URL在Instagram上共享图像吗?在Instagram上共享的意图采用Intent.EXTRA_STREAM参数,该参数应为media path on device. 如何将图片从网址转换为该类型?

But onBitmapLoaded is never being called. Is there any other way to share image on Instagram from a url? The intent which share on Instagram takes Intent.EXTRA_STREAM parameter which should be a media path on device. How do I convert an image from a url into that type?

推荐答案

毕加索仅保持对目标的弱引用,因此在您的情况下将被垃圾回收.结果,未调用onBitmapLoaded.

Picasso only keeps weak reference to target, so in your case it will be garbage collected. As a result, onBitmapLoaded is not being called.

您应该存储对目标的强引用(使目标成为类的成员).

You should store strong reference to target (make target member of your class).

这篇关于Android:不调用Picasso onBitMapLoaded加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆