trying to draw too large(106,975,232 bytes) bitmap.
Loading Large Bitmaps Efficiently
Note: There are several libraries that follow best practices for loading images. You can use these libraries in your app to load images in the most optimized manner. We recommend the Glide library, which loads and displays images as quickly and smoothly as possible. Other popular image loading libraries include Picasso from Square and Fresco from Facebook. These libraries simplify most of the complex tasks associated with bitmaps and other types of images on Android.
Images come in all shapes and sizes. In many cases they are larger than required for a typical application user interface (UI). For example, the system Gallery application displays photos taken using your Android devices's camera which are typically much higher resolution than the screen density of your device.
Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory. The lower resolution version should match the size of the UI component that displays it. An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.
This lesson walks you through decoding large bitmaps without exceeding the per application memory limit by loading a smaller subsampled version in memory.
Read Bitmap Dimensions and Type
The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class. Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.
Load a Scaled Down Version into Memory
Now that the image dimensions are known, they can be used to decide if the full image should be loaded into memory or if a subsampled version should be loaded instead. Here are some factors to consider:
- Estimated memory usage of loading the full image in memory.
- Amount of memory you are willing to commit to loading this image given any other memory requirements of your application.
- Dimensions of the target
ImageViewor UI component that the image is to be loaded into. - Screen size and density of the current device.
For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object. For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384. Loading this into memory uses 0.75MB rather than 12MB for the full image (assuming a bitmap configuration of ARGB_8888). Here’s a method to calculate a sample size value that is a power of two based on a target width and height:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2;
final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
} return inSampleSize;
}
Note: A power of two value is calculated because the decoder uses a final value by rounding down to the nearest power of two, as per the inSampleSize documentation.
To use this method, first decode with inJustDecodeBounds set to true, pass the options through and then decode again using the new inSampleSizevalue and inJustDecodeBounds set to false:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
This method makes it easy to load a bitmap of arbitrarily large size into an ImageView that displays a 100x100 pixel thumbnail, as shown in the following example code:
mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
trying to draw too large(106,975,232 bytes) bitmap.的更多相关文章
- java.lang.RuntimeException: Canvas: trying to draw too large(203212800bytes) bitmap.
https://www.cnblogs.com/spring87/p/7645625.html 今天我师父发现了一个问题:在更换登录页图片后,更新版本,部分手机打开会闪退.借了一个三星手机后,查看问题 ...
- java.lang.RuntimeException: Canvas: trying to draw too large(107331840bytes) bitmap.
环境: Android 8.0.1 MIUI 真机测试闪退 gradle 4.1 compileSdkVersion 26 buildToolsVersion '26.0.2' minSdkVersi ...
- 异常:java.lang.RuntimeException: Canvas: trying to draw too large(161740800bytes) bitmap
现象 今天做一个安卓项目的时候,我使用了10张图片,这10张图片都是放在了drawable目录下. 根据这个错误,我在网上寻找解决问题的方案,然后我放在了mipmap-xxhdpi下结果可以运行. 但 ...
- imageview无法显示图片:java.lang.RuntimeException: Canvas: trying to draw too large(281520000bytes) bitmap
图片太大需要压缩. 压缩方法:http://jingyan.baidu.com/article/cdddd41c3ef41153ca00e162.html 如果特别大(几十M),可以先用在线的图片压缩 ...
- HTML5资料
1 Canvas教程 <canvas>是一个新的用于通过脚本(通常是JavaScript)绘图的HTML元素.例如,他可以用于绘图.制作图片的组合或者简单的动画(当然并不那么简单).It ...
- MQTT_DEMO
1 /* 2 Copyright (c) 2009-2012 Roger Light <roger@atchoo.org> 3 All rights reserved. 4 5 Redis ...
- Git Submodule使用完整教程
Git Submodule功能刚刚开始学习可能觉得有点怪异,所以本教程把每一步的操作的命令和结果都用代码的形式展现给大家,以便更好的理解. 1.对于公共资源各种程序员的处理方式 每个公司的系统都会有一 ...
- Java程序员必备英文单词
列表中共有769个单词,这些单词是从JDK.Spring.SpringBoot.Mybatis的源码中解析得到,按照在源码中出现的频次依次排列,页面中的单词是出现频次大于1000的.单词的音标.翻译结 ...
- Ehcache(2.9.x) - Configuration Guide, Configuring Storage Tiers
About Storage Tiers Ehcache has three storage tiers, summarized here: Memory store – Heap memory tha ...
随机推荐
- HTML网页滚动加载--mark一下
console控制台: >: function stroll(){ window.scrollTo(, document.body.scrollHeight); }; >: window. ...
- 《从零开始搭建游戏服务器》Eclipse和Tomcat安装配置
我选择用来进行服务器开发的语言是Java,开发流程更接近于JavaWeb,所以需要先为开发配置一个开发环境,需要配置的主要是Eclipse和Tomcat(Web工程的容器或管理工具). 一.资源下载: ...
- Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)
报错如图 1.先检查当前windows账户用户名是否为全英文,没有就新建一个,大多数用户败在这一步,而官方也没有解释 如何新建:开始-->控制面板-->用户账户和家庭安全-->用户账 ...
- 微软自带的异步Ajax请求
一.使用步骤 二.示例代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- send to instance already dealloc nil error
这个是因为发送消息的对象已经被dealloc了,然后再次发送[release]请求就不行了.所以可以retain或者alloc对象 if (self.buttonsList) { ...
- C# 把控件内容导出图片
Bitmap newbitmap = new Bitmap(panelW.Width, panelW.Height); panelW.DrawToBitmap(newbitmap ...
- gdb生成的core文件位置
gdb可以生成core文件,记录堆栈信息,core文件名字是下面这种格式 :core.9488,其中9488是PID 文件位置是当前目录
- JBoss 6.1安装配置问题
一,配置环境变量 JBOSS_HOME:配置到解压文件的根文件夹下: classpath跟JAVA_HOME:配置的解压文件夹\bin文件夹以下: 二,訪问端口号 因为我之前安装过Tomcat,所以占 ...
- jquery 常用选择器 回顾 ajax() parent() parents() children() siblings() find() eq() has() filter() next()
1. $.ajax() ajax 本身是异步操作,当需要将 异步 改为 同步时: async: false 2.parent() 父级元素 和 parents() 祖先元素 的区别 parent ...
- ACM-ICPC如何起步[转]
ACM-ICPC如何起步 刚刚绝定投身ACM-ICPC的同学先要过两关. 第一关:程序设计语言 如果学校有开设相关课程,则省去了很多麻烦.如果没有则可以选择<程序设计导引及在线实践>作为教 ...