当前位置:网站首页>Solutions to oom caused by pictures in res directory
Solutions to oom caused by pictures in res directory
2022-07-24 19:23:00 【Martin-Rayman】
Reference link :http://blog.csdn.net/coderinchina/article/details/40964205
Reference link :http://blog.csdn.net/shineflowers/article/details/41648745
More pictures are needed in recent projects , And the picture takes up a lot of pixels and memory . Cause oneortwo pictures to come down directly OOM 了 .
For this difficult problem , I have referred to many blog posts these two days .
So let's first understand what happens because the picture is too large OOM
The memory allocated by the current machine for each process is about 32M, But when the phone memory is not enough to allocate 32M When ~ Or when this app The running memory of is greater than 32M Will cause OOM.
So let's focus on the picture first .
Android How to calculate the memory size of a picture in : The picture is long * wide * Number of pixel bytes occupied , And the number of pixel bytes Android There are only four kinds ,
1:ALPHA_8 Occupy 1 Bytes
2:ARGB_4444 Occupy 2 Bytes
3:ARGB_8888 Occupy 4 Bytes
4:RGB_565 Occupy 2 Bytes
transparency Red green Blue
And these bytes can be passed bitmap Object to set bitmap.setConfig(Bitmap.Config.ARGB_4444);
If this is a fixed value , Then change the size of a picture , You can only change the width or height , If only higher , If width does not change , It will deform the picture , Therefore, it is generally changed together , So the picture should be scaled , When zooming, it scales according to the width and height of the screen , because Android There's a lot of equipment , The width and height of each screen are also different , In this way, you can load large pictures to avoid OOM.
For example, a picture used in a recent project 1280*720 Pixel image , And it uses ARGB_8888, Then calculate the memory occupied :1280*720*4/1024/1024=3.5M A picture
So a few pictures like this can put Android Memory burst .
Looking at the above formula, you also know that loading pictures into memory is to see The image Pixels and quality determine its size in the running memory
Then it summarizes the best solution :
Compress compress or compress pictures ( But there are two compression methods )
The first way : Compress the scale of the picture ( Code reference to major websites )
We can set BitmapFactory.Optiions Of inJustDecodeBounds The attribute is true, In this case, the image will not be loaded into memory ,
But the picture will be width and height Properties are read , We can use this attribute to bitmap Compress .Options.inSampleSize The compression ratio can be set .
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
// The height and width of the source picture
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate the ratio of actual width to target width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Calculate scale
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {
// The first analysis will inJustDecodeBounds Set to true, To get the image size
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Call the method defined above to calculate inSampleSize value
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Use obtained inSampleSize Value parses the image again
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
} The second way : The quality of the compressed image
We know from the formula just mentioned , We can go through bitmap.setConfig(Bitmap.Config.ARGB_4444) To set specific Bitmap The display quality of . The following method refers to this method
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 12.5px; letter-spacing: 0.5px; line-height: 22.5px;"> public Bitmap readBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// Get resource pictures
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
} among resId It can be passed in directly Drawable Files in the file directory ( Don't put pictures with raw Under the table of contents )The following is what I met these days OOM Solutions for , The effect of the second method is quite good .
边栏推荐
- Installation and use of lsky Pro lancong drawing bed: a drawing bed program for online uploading and managing pictures
- Leetcode402 remove K digits
- Zooinspector Download
- First knowledge database
- Original reverse compensation and size end
- [question 39] special question for Niuke in-depth learning
- profile环境切换
- Equals() method of object class
- Convolutional Neural Networks in TensorFlow quizs on Coursera
- Virbox compiler, which supports source code encryption of the whole platform and architecture
猜你喜欢

OPENGL学习(二)OPENGL渲染管线

LTSpice software power settings

Machine learning_ Data processing and model evaluation
![[question 39] special question for Niuke in-depth learning](/img/18/0e182f2c003ff5dd8ed3751c718d73.png)
[question 39] special question for Niuke in-depth learning

Convolutional neural network CNN

day 3

Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack

Machine learning_ Softmax function (multi classification problem)

How to encrypt your own program with dongle

Leetcode652 finding duplicate subtrees
随机推荐
Nacos introduction and console service installation
Day 4 (item 1: household income and expenditure records)
Taokeeper environment setup
MySQL index principle and query optimization "suggestions collection"
JDBC batch inserts 100000 /1million pieces of data
Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
High speed ASIC packaging trends: integration, SKU and 25g+
Ebpf verifier
Tcl/tk file operation
SATA protocol OOB essay
Biopharmaceutical safety, power supply and production guarantee
Wireshark simple filter rule
How to encrypt your own program with dongle
Why are there loopholes in the website to be repaired
Detailed explanation of the relationship between MySQL tables
多线程与并发编程常见问题(未完待续)
PCIe link initialization & Training
2022 Summer Games of Hangzhou electric power multiple schools 1012aalice and Bob (game theory)
asp. Net coree file upload and download example
Configmanager of unity framework [JSON configuration file reading and writing]