【问题标题】:android: get image dimensions without opening itandroid:无需打开即可获取图像尺寸
【发布时间】:2012-06-20 21:27:05
【问题描述】:

我想在将它们加载到 RAM 之前获取存储在 sdcard 上的图像的宽度和高度(以像素为单位)。我需要知道大小,所以我可以在加载它们时相应地对它们进行下采样。如果不对它们进行下采样,我会得到 OutOfMemoryException。

有人知道如何获取图像文件的尺寸吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    将仅解码边界的选项传递给工厂:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    
    //Returns null, sizes are in the options variable
    BitmapFactory.decodeFile("/sdcard/image.png", options);
    int width = options.outWidth;
    int height = options.outHeight;
    //If you want, the MIME type will also be decoded (if possible)
    String type = options.outMimeType;
    

    【讨论】:

    • 如何获取密度值?
    • 高度和宽度返回 0
    • 如果 uri 类似于 "images/1234/get" 不包括图像格式,是否可以获得尺寸? @Devunwired
    • 这仍然处理图像,同时检查多个图像花费的时间太长,decodeFile 仍然处理整个图像,即使它没有为它分配内存。
    【解决方案2】:

    其实,还有另一种方法可以解决这个问题。使用下面的方法,我们可以避免文件和URI的麻烦。

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    ParcelFileDescriptor fd = mContext.getContentResolver().openFileDescriptor(u, "r"); // u is your Uri
    BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, options);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多