毕加索 - 如何获得真实的图像尺寸? [英] Picasso - how to get the real image size?

查看:186
本文介绍了毕加索 - 如何获得真实的图像尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Picasso库从URL加载图像。我想获得真实的图像大小,但我只能在内存中获取图像大小:

I load an image from URLusing Picasso library. I want to get the real image size, but I can only get the image size in memory:

Picasso.with(this)
    .load(imageUrl)
    .error(R.drawable.no_image)
    .into(photoView, new Callback() {
        @Override
        public void onSuccess() {
            Bitmap bitmap = ((BitmapDrawable)photoView.getDrawable()).getBitmap();
            textImageDetail.setText(bitmap.getByteCount());// image size on memory, not actual size of the file
        }

        @Override
        public void onError() { }
    });

如何获取加载图像的大小?我认为它存储在缓存中的某个地方,但我不知道如何访问图像文件。

How to get the size of the loaded image? I think it is stored somewhere in a cache, but I do not know how to access the image file.

更新

对不起我的英语不好,也许我问了错误的问题。我需要获得图像大小(128 kb,2 MB等)。 图像分辨率(800x600等)

Sorry for my bad English, maybe I asked the wrong question. I need to get the image size (128 kb, 2 MB, etc.). NOT the image resolution (800x600, etc.)

推荐答案

您可以先获得实际的位图正在加载的图像,然后找到它的尺寸。这必须在 AsyncTask 之类的异步方法中运行,因为下载图像是同步的。下面是一个示例:

You could first get the actual Bitmap image that is getting loaded, and then find the dimensions of that. This has to be run in an asynchronous method like AsyncTask because downloading the image is synchronous. Here is an example:

Bitmap downloadedImage = Picasso.with(this).load(imageUrl).get();
int width = downloadedImage.getWidth();
int height = downloadedImage.getHeight();

如果你想获得 Bitmap的实际图像大小(以字节为单位) / code>,只需使用

If you want to get the actual image size in bytes of the Bitmap, just use

// In bytes
int bitmapSize = downloadedImage.getByteCount();
// In kilobytes
double kbBitmapSize = downloadedImage.getByteCount() / 1000;

用您想要的任何网址替换 imageUrl 使用。希望它有所帮助!

Replace the imageUrl with whatever URL you want to use. Hope it helps!

这篇关于毕加索 - 如何获得真实的图像尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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