BitmapFactory.decodeResource 返回为 null 的问题

时间: 2023-12-06 14:41:11 浏览: 47
BitmapFactory.decodeResource() 可能返回 null 的原因有以下几个: 1.资源文件不存在:请检查资源文件是否存在。如果不存在,那么 decodeResource() 方法将返回 null。 2.资源文件过大:如果资源文件过大,则可能会导致内存不足。在这种情况下,decodeResource() 方法也会返回 null。可以尝试使用 BitmapFactory.Options 对象中的 inSampleSize 属性来减小图片的大小,从而避免内存不足的问题。 3.图片格式不支持:如果资源文件的图片格式不受支持,则 decodeResource() 方法也会返回 null。Android 支持的图片格式包括 PNG、JPEG、GIF、BMP、WebP 等格式。 4.内存泄漏:如果在使用 Bitmap 对象后没有正确释放它,那么可能会导致内存泄漏。在这种情况下,decodeResource() 方法也会返回 null。请确保在使用完成后使用 Bitmap.recycle() 方法释放 Bitmap 对象。 总之,如果你遇到了 BitmapFactory.decodeResource() 返回 null 的问题,可以先检查上述几个原因,找出问题所在并解决它。
相关问题

BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher)返回为null

如果 `BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)` 返回 null,可能有以下几种原因: 1. R.mipmap.ic_launcher 不存在或被删除。 2. ic_launcher 图片过大,导致内存溢出。可以尝试使用 `BitmapFactory.Options` 对象来缩小图片大小。 3. ic_launcher 的格式不受支持。`BitmapFactory.decodeResource` 只支持 JPEG、PNG、WebP 和 GIF 格式的图片。 4. 程序中存在内存泄漏,导致内存不足。可以尝试手动调用 `Bitmap.recycle()` 方法释放内存。 您可以尝试以下方法进行排查: 1. 检查 R.mipmap.ic_launcher 是否存在并且拼写正确。 2. 尝试使用 `BitmapFactory.Options` 对象进行图片大小的缩放。 3. 尝试使用其他图片格式,如 JPEG 或 PNG。 4. 检查程序中是否存在内存泄漏的情况。您可以使用 Android Studio 自带的 Memory Profiler 工具来进行检查。

BitmapFactory.Options如何使用

`BitmapFactory.Options` 是一个用于配置位图加载选项的类。使用 `BitmapFactory.Options` 可以控制位图的加载方式,如缩放、解码格式、采样率等。以下是一个简单的使用示例: ```java // 加载图片的选项 BitmapFactory.Options options = new BitmapFactory.Options(); // 设置为 true,只获取图片的宽度和高度,不加载图片到内存中 options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.drawable.my_image, options); // 计算采样率 int sampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 设置采样率 options.inSampleSize = sampleSize; // 关闭只获取图片宽度和高度的选项 options.inJustDecodeBounds = false; // 加载图片到内存中 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image, options); ``` 以上示例中,首先使用 `inJustDecodeBounds` 选项将 `BitmapFactory.decodeResource()` 方法的返回值设置为 null,并且只获取图片的宽度和高度,而不是真正地将图片加载到内存中。接着,根据目标宽度和高度计算采样率,然后将采样率设置到 `inSampleSize` 选项中。最后,关闭 `inJustDecodeBounds` 选项并重新调用 `BitmapFactory.decodeResource()` 方法来加载图片到内存中。 注意,`calculateInSampleSize()` 方法需要自己实现。它的作用是根据目标宽度和高度计算出最接近的采样率。以下是一个示例实现: ```java private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 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; while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; } ``` 以上示例中,`calculateInSampleSize()` 方法会根据图片的宽度和高度以及目标宽度和高度计算出最接近的采样率。

相关推荐

package com.blog.dragview; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.util.Log; import com.blog.R; import org.json.JSONArray; import org.json.JSONException; import java.util.List; public class MainActivity extends Activity{ private DrawingView _view; @Override protected void onCreate(Bundle savedInstanceState) { _view = new DrawingView(this); super.onCreate(savedInstanceState); setContentView(_view); Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.hengzhegou); Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.pie); CustomBitmap customBitmap1 = new CustomBitmap(bmp1); CustomBitmap customBitmap2 = new CustomBitmap(bmp2); customBitmap1.setId(1); customBitmap2.setId(2); if (getSavedMatrix(1) != null){ Log.e("tag", "matrix 1 is not null"); customBitmap1.setMatrix(getSavedMatrix(1)); } if (getSavedMatrix(2) != null){ Log.e("tag", "matrix 2 is not null"); customBitmap2.setMatrix(getSavedMatrix(2)); } _view.addBitmap(customBitmap1); _view.addBitmap(customBitmap2); } //����matrix private void saveMatrix(CustomBitmap customBitmap){ Log.e("tag", "save matrix" + customBitmap.getId()); SharedPreferences.Editor editor = getSharedPreferences("matrix", Context.MODE_PRIVATE).edit(); Matrix matrix = customBitmap.matrix; float[] values = new float[9]; matrix.getValues(values); JSONArray array = new JSONArray(); for (float value:values){ try { array.put(value); } catch (JSONException e) { e.printStackTrace(); } } editor.putString(String.valueOf(customBitmap.getId()), array.toString()); editor.commit(); Log.e("tag", "save matrix id:" + customBitmap.getId() + "---------"+values[Matrix.MPERSP_0] + " , " + values[Matrix.MPERSP_1] + " , " + values[Matrix.MPERSP_2] + " , " + values[Matrix.MSCALE_X] + " , " + values[Matrix.MSCALE_Y] + " , " + values[Matrix.MSKEW_X] + " , " + values[Matrix.MSKEW_Y] + " , " +values[Matrix.MTRANS_X] + " , " + values[Matrix.MTRANS_Y]); } //��ȡmatrix private Matrix getSavedMatrix(int id){ SharedPreferences sp = getSharedPreferences("matrix", Context.MODE_PRIVATE); String result = sp.getString(String.valueOf(id), null); if (result != null){ float[] values = new float[9]; Matrix matrix = new Matrix(); try { JSONArray array = new JSONArray(result); for (int i = 0; i < array.length(); i++) { values[i] = Float.valueOf(String.valueOf(array.getDouble(i))); } matrix.setValues(values); } catch (JSONException e) { e.printStackTrace(); } Log.e("tag", "get matrix id:" + id + "---------"+values[Matrix.MPERSP_0] + " , " + values[Matrix.MPERSP_1] + " , " + values[Matrix.MPERSP_2] + " , " + values[Matrix.MSCALE_X] + " , " + values[Matrix.MSCALE_Y] + " , " + values[Matrix.MSKEW_X] + " , " + values[Matrix.MSKEW_Y] + " , " +values[Matrix.MTRANS_X] + " , " + values[Matrix.MTRANS_Y]); return matrix ; } return null; } @Override public void finish() { List<CustomBitmap> list = _view.getViews(); for (CustomBitmap customBitmap:list){ saveMatrix(customBitmap); } super.finish(); } }

最新推荐

recommend-type

HP-Socket编译-Linux

HP-Socket编译-Linux
recommend-type

JavaScript_生活在Discord上的开源社区列表.zip

JavaScript
recommend-type

JavaScript_MultiOn API.zip

JavaScript
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这