android处理图片的一些问题总结
2014-06-11 14:12
579 查看
第一个问题是out of memory
这个据说是VM对一个程序申请的所有的bitmap对象会有一个最大值的要求。解决这个问题有几个方法:
1. 从源文件生成图片时,直接将图片缩小,而不是加载原始大小的图片。如下代码:
2. 及时删除不需要使用的bitmap对象,不要将所有的对象都cache住
3. 增加程序的heap size。从某个版本开始,android manifest文件里有一个新的属性了:
android:largeHeap
第二个问题是
这个问题下面链接有详细描述:
http://stackoverflow.com/questions/7428996/hw-accelerated-activity-how-to-get-opengl-texture-size-limit
简单说就是硬件加速的时候,对图片的大小有限制。不同设备可能有不同的最大值。这个问题悲催的地方是,程序貌似没有捕获到这个exception, 结果是程序也不报错,图片也显示不出来。只有看debug log才能发现这个error message.
一个解决的方法是禁止硬件加速,简单粗暴:
比较好的解决方法是类似google map的实现:将图片分成不同的块,每次加载需要的块。android提供了一个方法:
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
采取上述操作后,就可以加载很多图片,同时也可以显示超级大图了。
1 | java.lang.OutOfMemoryError: bitmap size exceedsVM budget |
1. 从源文件生成图片时,直接将图片缩小,而不是加载原始大小的图片。如下代码:
01 | Bitmap bitmap = null ; |
02 | if (mUseZoomOut || mUseZoomIn) { |
03 | // decode image size (decode metadata only, not the whole image) |
04 | o = new BitmapFactory.Options(); |
05 | o.inJustDecodeBounds = true ; |
06 | stream = new FileInputStream(filename); |
07 | BitmapFactory.decodeStream(stream, null , o); |
08 | stream.close(); |
09 |
10 | // get original image size |
11 | int inWidth = o.outWidth; |
12 | int inHeight = o.outHeight; |
13 | clog(String.format( "Original bitmap size: (%dx%d)." , inWidth, inHeight)); |
14 |
15 | // get size for pre-resized image |
16 | o = new Options(); |
17 | o.inSampleSize = Math.max(inWidth/targetWidth, inHeight/targetHeight); |
18 | } |
19 |
20 | // decode pre-resized image |
21 | stream = new FileInputStream(filename); |
22 | // o.inPurgeable = true; |
23 | bitmap null , o); |
24 | stream.close(); |
25 | clog(String.format( "Pre-sized bitmap size: (%dx%d)." , bitmap.getWidth(), bitmap.getHeight())); |
3. 增加程序的heap size。从某个版本开始,android manifest文件里有一个新的属性了:
1 | android:largeHeap= "true" |
1 | Whether your application 's processesshould be created with a large Dalvik heap. Thisappliesto all processescreated for the application. It only appliesto the first application loaded into a process; if you' re using a shared user ID to allow multiple applicationsto use a process, they all must use this option consistently or they will have unpredictable results. |
2 | Most appsshould not need this and should instead focuson reducing their overall memory usage for improved performance. Enabling this also doesnot guarantee a fixed increase in available memory, because some devicesare constrained by their total available memory. |
3 | To query the available memory size at runtime, use the methodsgetMemoryClass() or getLargeMemoryClass(). |
1 | Bitmap too large to be uploaded into a texture exception |
http://stackoverflow.com/questions/7428996/hw-accelerated-activity-how-to-get-opengl-texture-size-limit
简单说就是硬件加速的时候,对图片的大小有限制。不同设备可能有不同的最大值。这个问题悲催的地方是,程序貌似没有捕获到这个exception, 结果是程序也不报错,图片也显示不出来。只有看debug log才能发现这个error message.
一个解决的方法是禁止硬件加速,简单粗暴:
1 | android:hardwareAccelerated= "false" |
2 |
3 | android:hardwareAccelerated |
4 | Whether or not hardware-accelerated rendering should be enabled for all activitiesand viewsin this application — "true" if it should be enabled, and "false" if not. The default value is "true" if you 've set either minSdkVersion or targetSdkVersion to "14" or higher; otherwise, it' s "false" . |
5 | Starting from Android 3.0 (API level 11 ), for many for applications that do not |
6 | Note |
7 | For more information, read the Hardware Acceleration guide. |
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
1 | public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) |
2 |
3 | public Bitmap |
相关文章推荐
- android处理图片的一些问题总结
- android处理图片的一些问题总结
- Android Bitmap操作内存问题总结(图片处理、截屏等)
- Android:相机适配及图片处理的一些问题
- Android下图片处理的的一些方法
- Android下图片处理的的一些方法
- android 针对于GridView中的getView方法的bug,使用本地缓存来处理图片显示的问题
- android 软件引用处理图片内在溢出问题
- Android 图片 OOM问题总结
- ASP.NET图片处理问题总结
- ASP.NET图片处理问题总结
- android大型数据库(sqlite)处理及其他问题总结
- android 一些相关处理网络图片与音乐(部分转载)
- Android 异步获取网络图片并处理导致内存溢出问题解决方法
- Android 异步获取网络图片并处理图片Out Of Memory 内存溢出问题
- ASP.NET图片处理问题总结
- android在处理图片资源的时候,进行一些类型的转换
- Android图片处理的的一些方法 - ORIETech
- android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下
- 工作中itext遇到的一些问题,做一下处理总结(2012.06.19)