简体   繁体   English

Android:将存储中的大图像加载到ImageView中

[英]Android: Load large image from storage into ImageView

I'm doing a gallery image. 我正在做画廊图片。 I get path of image in device and parse to URI. 我在设备中获取图像的路径并解析为URI。 Then I use Picasso Android Lib to load image into Imageview in Gridview . 然后,我使用Picasso Android Lib将图像加载到Gridview Imageview中。 It's work fine until have a large image. 直到获得大图像之前,它都可以正常工作。 Picasso can not load large image. 毕加索无法加载大图像。 I got error Out Of Memory . Out Of Memory Is there any suggestion to load large image into ImageView ? 有什么建议将大图像加载到ImageView吗? And have any lib to load image into ImageView can instead Picasso? 并有任何库可以将图像加载到ImageView来代替毕加索吗?

I found ImageLoader lib for my problem. 我找到了我的问题的ImageLoader lib。 It works fine. 工作正常。 I tested on my project, and then I saw that ImageLoader looks better than Picasso. 我在项目上进行了测试,然后发现ImageLoader看起来比毕加索更好。

Learn how to use common techniques to process and load Bitmap objects in a way that keeps your user interface (UI) components responsive and avoids exceeding your application memory limit. 了解如何使用常见技术来处理和加载Bitmap对象,从而保持用户界面(UI)组件的响应速度,并避免超出应用程序内存限制。 If you're not careful, bitmaps can quickly consume your available memory budget leading to an application crash due to the dreaded exception: java.lang.OutofMemoryError: bitmap size exceeds VM budget. 如果不小心,由于可怕的异常,位图可能会迅速消耗可用的内存预算,从而导致应用程序崩溃:java.lang.OutofMemoryError:位图的大小超出了VM预算。

There are a number of reasons why loading bitmaps in your Android application is tricky: 在您的Android应用程序中加载位图很麻烦的原因有很多:

Mobile devices typically have constrained system resources. 移动设备通常具有受限的系统资源。 Android devices can have as little as 16MB of memory available to a single application. Android设备最多可为单个应用程序提供16MB的内存。 The Android Compatibility Definition Document (CDD), Section 3.7. Android兼容性定义文档(CDD),第3.7节。 Virtual Machine Compatibility gives the required minimum application memory for various screen sizes and densities. 虚拟机兼容性为各种屏幕尺寸和密度提供了所需的最小应用程序内存。 Applications should be optimized to perform under this minimum memory limit. 应优化应用程序以在此最小内存限制下执行。 However, keep in mind many devices are configured with higher limits. 但是,请记住,许多设备都配置了更高的限制。

Bitmaps take up a lot of memory, especially for rich images like photographs. 位图占用大量内存,特别是对于照片等丰富的图像。 For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). 例如,Galaxy Nexus上的相机拍摄的照片最大为2592x1936像素(5兆像素)。 If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices. 如果使用的位图配置为ARGB_8888(Android 2.3及更高版本的默认设置),则将该图像加载到内存中大约需要19MB的内存(2592 * 1936 * 4字节),从而立即耗尽某些设备上每个应用程序的限制。 Android app UI's frequently require several bitmaps to be loaded at once. Android应用程序UI经常需要一次加载多个位图。 Components such as ListView, GridView and ViewPager commonly include multiple bitmaps on-screen at once with many more potentially off-screen ready to show at the flick of a finger. 诸如ListView,GridView和ViewPager之类的组件通常包含一次在屏幕上显示多个位图的功能,还有更多可能在屏幕外显示的信息,只需轻轻一击即可显示。

Read More Regarding this issue 阅读更多有关此问题的信息

Here is an example I once used, but it is not perfect! 这是我曾经使用过的示例,但这并不完美! (Sorry) (抱歉)

You can reduce the bitmap size: 您可以减小位图的大小:

public Bitmap resizeBitmap(Bitmap bitmap) {
    if (bitmap.getHeight() > 4096 || bitmap.getWidth() > 4096) {
        int width = (int) (bitmap.getWidth() * 0.9);
        int height = (int) (bitmap.getHeight() * 0.9);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        resizeBitmap(resizedBitmap);
        returnresizedBitmap;
    } else {
        return bitmap;
    }
}

If I should do it again: (not tested) 如果我应该再做一次:(未测试)

public Bitmap resizeBitmap(Bitmap bitmap) {
    int originalWidth = bitmap.getWidth();
    int originalHeight = bitmap.getHeight();

    if (originalWidth > 4096 || originalHeight  > 4096) {

        int height;
        int width;

        if(originalHeight > originalWidth) {
            height = 4096;
            width  = originalWidth  / (originalHeight / 4096);
        } else {
            width  = 4096;
            height = originalHeight / (originalWidth / 4096);
        }

        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        return resizedBitmap;
    } else {
        return bitmap;
    }
}

You can custom recent-images library for your purpose. 您可以根据需要自定义近期图像库。 It's very simple and easy to use library. 它非常简单易用。 It creates a thumbnail of image for showing in gridview and then in click opens the original. 它创建图像的缩略图以显示在gridview中,然后单击以打开原始图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM