从装载Picasso的ImageView获取Bitmap [英] Get Bitmap from ImageView loaded with Picasso

查看:482
本文介绍了从装载Picasso的ImageView获取Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法可以加载图像,如果图像尚未加载,它将在服务器上查找它。然后将其存储在apps文件系统中。如果它在文件系统中,则会加载该图像,因为这比从服务器中提取图像要快得多。如果您在未关闭应用程序之前加载了图像,它将存储在静态字典中,以便可以在不占用更多内存的情况下重新加载,以避免内存不足错误。

I had a method that loads images, if the image has not been loaded before it will look for it on a server. Then it stores it in the apps file system. If it is in the file system it loads that image instead as that is much faster than pulling the images from the server. If you have loaded the image before without closing the app it will be stored in a static dictionary so that it can be reloaded without using up more memory, to avoid out of memory errors.

这一切都很好,直到我开始使用Picasso图像加载库。现在我将图像加载到ImageView中,但我不知道如何获取返回的Bitmap,以便我可以将其存储在文件或静态字典中。这使事情变得更加困难。因为这意味着它每次都试图从服务器加载图像,这是我不想发生的事情。有没有办法在将Bitmap加载到ImageView后得到它?下面是我的代码:

This all worked fine until I started using the Picasso image loading library. Now I'm loading images into an ImageView but I don't know how to get the Bitmap that is returned so that I can store it in a file or the static dictionary. This has made things more difficult. because it means it tries to load the image from the server every time, which is something I don't want to happen. Is there a way I can get the Bitmap after loading it into the ImageView? Below is my code:

public Drawable loadImageFromWebOperations(String url,
        final String imagePath, ImageView theView, Picasso picasso) {
    try {
        if (Global.couponBitmaps.get(imagePath) != null) {
            scaledHeight = Global.couponBitmaps.get(imagePath).getHeight();
            return new BitmapDrawable(getResources(),
                    Global.couponBitmaps.get(imagePath));
        }
        File f = new File(getBaseContext().getFilesDir().getPath()
                .toString()
                + "/" + imagePath + ".png");

        if (f.exists()) {
            picasso.load(f).into(theView);
            **This line below was my attempt at retrieving the bitmap however 
            it threw a null pointer exception, I assume this is because it 
            takes a while for Picasso to add the image to the ImageView** 
            Bitmap bitmap = ((BitmapDrawable)theView.getDrawable()).getBitmap();
            Global.couponBitmaps.put(imagePath, bitmap);
            return null;
        } else {
            picasso.load(url).into(theView);
            return null;
        }
    } catch (OutOfMemoryError e) {
        Log.d("Error", "Out of Memory Exception");
        e.printStackTrace();
        return getResources().getDrawable(R.drawable.default1);
    } catch (NullPointerException e) {
        Log.d("Error", "Null Pointer Exception");
        e.printStackTrace();
        return getResources().getDrawable(R.drawable.default1);
    }
}

非常感谢任何帮助,谢谢!

Any help would be greatly appreciated, thank you!

推荐答案

使用Picasso,您不需要实现自己的静态字典,因为它是自动完成的。 @intrepidkarthi是正确的,因为Picasso第一次加载时会自动缓存图像,因此它不会经常调用服务器来获取相同的图像。

With Picasso you don't need to implement your own static dictionary because it is done automatically for you. @intrepidkarthi was right in the sense that the images are automatically cached by Picasso the first time they are loaded and so it doesn't constantly call the server to get the same image.

话虽如此,我发现自己处于类似的情况:我需要访问下载的位图,以便将其存储在应用程序的其他位置。为此,我稍微调整了@Gilad Haimov的答案:

That being said, I found myself in a similar situation: I needed to access the bitmap that was downloaded in order to store it somewhere else in my application. For that, I tweaked @Gilad Haimov's answer a bit:

Java,旧版Picasso:

Java, with an older version of Picasso:

Picasso.with(this)
    .load(url)
    .into(new Target() {

        @Override
        public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from) {
            /* Save the bitmap or do something with it here */

            // Set it in the ImageView
            theView.setImageBitmap(bitmap); 
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {}

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {}
});

Kotlin,Picasso版本为2.71828:

Kotlin, with version 2.71828 of Picasso:

Picasso.get()
        .load(url)
        .into(object : Target {

            override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                /* Save the bitmap or do something with it here */

                // Set it in the ImageView
                theView.setImageBitmap(bitmap)
            }

            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}

            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}

        })

这使您可以在加载位图时访问加载的位图异步。您只需记住在ImageView中设置它,因为它不再自动为您完成。

This gives you access to the loaded bitmap while still loading it asynchronously. You just have to remember to set it in the ImageView as it's no longer automatically done for you.

在旁注中,如果您想知道图像的来源,可以使用相同的方法访问该信息。上面方法的第二个参数,来自的Picasso.LoadedFrom,是一个枚举,可以让你知道从哪个源加载位图(三个来源是 DISK MEMORY NETWORK 。( Source )。Square Inc.也提供了一种视觉方式通过使用此处解释的调试指示器来查看位图的加载位置。

On a side note, if you're interested in knowing where your image is coming from, you can access that information within that same method. The second argument of the above method, Picasso.LoadedFrom from, is an enum that lets you know from what source the bitmap was loaded from (the three sources being DISK, MEMORY, and NETWORK. (Source). Square Inc. also provides a visual way of seeing where the bitmap was loaded from by using the debug indicators explained here.

希望这会有所帮助!

这篇关于从装载Picasso的ImageView获取Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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