Android 开发错误集锦

1. eclipse的Device中不显示手机


在eclipse中连接不上手机,出现adb server didn't ACK  fail to start daemon 错误.

出现这种原因是因为adb所使用的5037端口被占用了.

使用adb kill-server 关闭adb服务 , 使用adb start-server开启adb命令 ; 如果5037端口被占用则会出现启动失败, 报出上述错误;

查看哪些进程占用了端口 : netstat -ano | findstr "5037" 会出现下面的结果

G:\Documents and Settings\Administrator>netstat -ano | findstr "5037"
  TCP    127.0.0.1:1939         127.0.0.1:5037         ESTABLISHED     5748
  TCP    127.0.0.1:1978         127.0.0.1:5037         ESTABLISHED     3000
  TCP    127.0.0.1:5037         0.0.0.0:0              LISTENING       5748
  TCP    127.0.0.1:5037         127.0.0.1:1939         ESTABLISHED     5748
  TCP    127.0.0.1:5037         127.0.0.1:1977         CLOSE_WAIT      5748
  TCP    127.0.0.1:5037         127.0.0.1:1978         ESTABLISHED     5748

可以看到 5748 3000 两个进程占用了5037端口.

打开进程管理器, 点击查看,选择列,将PID进程标示符勾选上,然后根据pid终止相应的进程.

这两个进程分别是豌豆荚 和 QQ , 同时将adb.exe 和 tadb.exe 进程干掉.

此时执行 adb start-server 命令即可启动adb服务 , 之后启动就会启动成功.



2. java.lang.OutOfMemoryError: thread creation failed 错误


10-17 09:51:58.830: E/AndroidRuntime(15695): FATAL EXCEPTION: Thread-316
10-17 09:51:58.830: E/AndroidRuntime(15695): java.lang.OutOfMemoryError: thread creation failed
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at java.lang.VMThread.create(Native Method)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at java.lang.Thread.start(Thread.java:1050)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.alarm.AlarmPlayer.enqueue(AlarmPlayer.java:201)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.alarm.AlarmPlayer.resume(AlarmPlayer.java:192)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.director.MpmDirector.filterMpmHealthData(MpmDirector.java:270)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.director.MpmDirector.onHealthDataDecoded(MpmDirector.java:72)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.decoder.BaseDecoder.notifyAllDataDecodedListener(BaseDecoder.java:89)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.decoder.MpmEcgDecoder.caculateEcgResult(MpmEcgDecoder.java:394)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.decoder.MpmEcgDecoder.casulateResult(MpmEcgDecoder.java:269)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.decoder.MpmEcgDecoder.processBigPackageOfEcgData(MpmEcgDecoder.java:100)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.decoder.MpmEcgDecoder.processData(MpmEcgDecoder.java:87)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.decoder.MpmDecoder.operator(MpmDecoder.java:135)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.BqfjApplication$2.receiveData(BqfjApplication.java:148)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.bluetooth.BqfjBluetoothManager.notifyReceivers(BqfjBluetoothManager.java:281)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.bluetooth.BqfjConnectionManager$DataPump.run(BqfjConnectionManager.java:397)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at com.ivt.mHealth.util.ReusableThread.run(ReusableThread.java:85)
10-17 09:51:58.830: E/AndroidRuntime(15695): 	at java.lang.Thread.run(Thread.java:856)
10-17 09:51:58.920: V/StagefrightPlayer(1875): isPlaying
10-17 09:51:58.920: V/MediaPlayerService(1875): [20] isPlaying: 0
10-17 09:51:58.920: V/MediaPlayer(15695): isPlaying: 0
10-17 09:51:58.920: V/MediaPlayer-JNI(15695): isPlaying: 0
10-17 09:51:58.925: D/dalvikvm(2219): GC_FOR_ALLOC freed 1595K, 30% free 26891K/37895K, paused 77ms, total 77ms
10-17 09:51:58.930: E/SQLiteLog(15695): (14) cannot open file at line 30241 of [00bb9c9ce4]
10-17 09:51:58.930: E/SQLiteLog(15695): (14) os_unix.c:30241: (2) open(/NotificationPermissions.db) - 
10-17 09:51:58.930: D/STATUSBAR-NetworkController(2441): refreshSignalCluster: data=-1 bt=false


开发中遇到了上述错误 , 后来发现是 使用了TraceView导致的

不知道在代码的什么地方添加了TraceView的相关代码 , 去掉之后 , 就好了

我在Activity中的onCreate()方法中添加了 : 

Debug.startMethodTracing();

在onDestroy()方法中添加了 :

Debug.stopMethodTracing();

去掉了上述两行代码 , 就不会出现上述错误了 ;



3. Call requires API level 8 (current min is 1)错误


导入了一个程序 , 每次运行之后都会出现该错误 .

点击clean 错误就会消失 , 但是执行该错误的时候该错误就会重新出现 . 

这个错误需要在AndroidManifest.xml配置文件中修改 user-sdk 标签

给user-sdk 添加一个android:minSdkVersion="8" 属性



4. 奇怪的空指针异常


今天出现一个奇怪的错误, 很简单空指针异常, 我只负责编码, 不管编译, 然后上传到SVN上, 结果出了下面的错误 , 看了半天 , 木有看懂 : 


11-13 10:39:53.337: E/AndroidRuntime(11030): FATAL EXCEPTION: Thread-394
11-13 10:39:53.337: E/AndroidRuntime(11030): java.lang.NullPointerException
11-13 10:39:53.337: E/AndroidRuntime(11030): 	at com.ivt.mHealth.widget.aa.b(Unknown Source)
11-13 10:39:53.337: E/AndroidRuntime(11030): 	at com.ivt.mHealth.widget.DisplaySurfaceView.a(Unknown Source)
11-13 10:39:53.337: E/AndroidRuntime(11030): 	at com.ivt.mHealth.widget.ae.run(Unknown Source)

我一看 蒙了 , 这是什么错误 , 没定义过这个类啊 , 后来才想起来 这是代码混淆以后的效果 . 

有点无语 ... 想了半小时才反应过来 

我直接从SVN上down了一下代码, 重新编译下 , ╮(╯▽╰)╭ 拨云见日 

11-13 10:41:57.432: E/AndroidRuntime(11507): FATAL EXCEPTION: Thread-398
11-13 10:41:57.432: E/AndroidRuntime(11507): java.lang.NullPointerException
11-13 10:41:57.432: E/AndroidRuntime(11507): 	at com.ivt.mHealth.widget.BpmSurface.drawMpmBpm(BpmSurface.java:169)
11-13 10:41:57.432: E/AndroidRuntime(11507): 	at com.ivt.mHealth.widget.BpmSurface.drawToSurfaceView(BpmSurface.java:109)
11-13 10:41:57.432: E/AndroidRuntime(11507): 	at com.ivt.mHealth.widget.DisplaySurfaceView.drawToSurface(DisplaySurfaceView.java:183)
11-13 10:41:57.432: E/AndroidRuntime(11507): 	at com.ivt.mHealth.widget.DisplaySurfaceView.access$1(DisplaySurfaceView.java:178)
11-13 10:41:57.432: E/AndroidRuntime(11507): 	at com.ivt.mHealth.widget.DisplaySurfaceView$RenderThread.run(DisplaySurfaceView.java:144)

以后千万不要看编译出来的产品的log , 都是混效过的 ........ 


这是少上传了一行代码到SVN上啊 ...... 泪奔 .... 半小时就这么浪费了


5. 内存溢出错误 


adb shell dumpsys meminfo com.ivt.mHealth


08-12 12:48:52.798: I/dalvikvm-heap(15260): Clamp target GC heap from 64.996MB to 64.000MB
08-12 12:48:52.798: D/dalvikvm(15260): GC_FOR_ALLOC freed 1K, 3% free 63863K/65543K, paused 190ms, total 190ms
08-12 12:48:52.798: I/dalvikvm-heap(15260): Forcing collection of SoftReferences for 21473-byte allocation
08-12 12:48:52.988: I/dalvikvm-heap(15260): Clamp target GC heap from 64.987MB to 64.000MB
08-12 12:48:52.988: D/dalvikvm(15260): GC_BEFORE_OOM freed 9K, 3% free 63853K/65543K, paused 186ms, total 186ms
08-12 12:48:52.988: E/dalvikvm-heap(15260): Out of memory on a 21473-byte allocation.
08-12 12:48:52.988: I/dalvikvm(15260): "main" prio=5 tid=1 RUNNABLE
08-12 12:48:52.988: I/dalvikvm(15260):   | group="main" sCount=0 dsCount=0 obj=0x40efd508 self=0x40d62778
08-12 12:48:52.988: I/dalvikvm(15260):   | sysTid=15260 nice=0 sched=0/0 cgrp=apps handle=1074577200
08-12 12:48:52.988: I/dalvikvm(15260):   | schedstat=( 21710980394 3466435126 36123 ) utm=1846 stm=324 core=1
08-12 12:48:52.988: I/dalvikvm(15260):   at com.ivt.mHealth.util.ByteArrayBuffer.expand(ByteArrayBuffer.java:~28)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.ivt.mHealth.util.ByteArrayBuffer.append(ByteArrayBuffer.java:62)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.ivt.mHealth.util.BqfjHttpHeaderPacker.packHttpHeader(BqfjHttpHeaderPacker.java:185)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.ivt.mHealth.datacommunication.impl.BqfjSendRTMpmDataMessageHandler$PacketTransController.sendPacket(BqfjSendRTMpmDataMessageHandler.java:770)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.ivt.mHealth.datacommunication.impl.BqfjSendRTMpmDataMessageHandler$PacketTransController.onPacketReceived(BqfjSendRTMpmDataMessageHandler.java:814)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.ivt.mHealth.datacommunication.impl.BqfjSendRTMpmDataMessageHandler$1.handleMessage(BqfjSendRTMpmDataMessageHandler.java:526)
08-12 12:48:52.988: I/dalvikvm(15260):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 12:48:52.988: I/dalvikvm(15260):   at android.os.Looper.loop(Looper.java:137)
08-12 12:48:52.988: I/dalvikvm(15260):   at android.app.ActivityThread.main(ActivityThread.java:4921)
08-12 12:48:52.988: I/dalvikvm(15260):   at java.lang.reflect.Method.invokeNative(Native Method)
08-12 12:48:52.988: I/dalvikvm(15260):   at java.lang.reflect.Method.invoke(Method.java:511)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-12 12:48:52.988: I/dalvikvm(15260):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-12 12:48:52.988: I/dalvikvm(15260):   at dalvik.system.NativeStart.main(Native Method)
08-12 12:48:52.988: D/AndroidRuntime(15260): Shutting down VM
08-12 12:48:52.988: W/dalvikvm(15260): threadid=1: thread exiting with uncaught exception (group=0x40efc2a0)
08-12 12:48:52.993: V/BluetoothSocket.cpp(15260): readNative
08-12 12:48:52.993: E/AndroidRuntime(15260): FATAL EXCEPTION: main
08-12 12:48:52.993: E/AndroidRuntime(15260): java.lang.OutOfMemoryError
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.ivt.mHealth.util.ByteArrayBuffer.expand(ByteArrayBuffer.java:28)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.ivt.mHealth.util.ByteArrayBuffer.append(ByteArrayBuffer.java:62)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.ivt.mHealth.util.BqfjHttpHeaderPacker.packHttpHeader(BqfjHttpHeaderPacker.java:185)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.ivt.mHealth.datacommunication.impl.BqfjSendRTMpmDataMessageHandler$PacketTransController.sendPacket(BqfjSendRTMpmDataMessageHandler.java:770)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.ivt.mHealth.datacommunication.impl.BqfjSendRTMpmDataMessageHandler$PacketTransController.onPacketReceived(BqfjSendRTMpmDataMessageHandler.java:814)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.ivt.mHealth.datacommunication.impl.BqfjSendRTMpmDataMessageHandler$1.handleMessage(BqfjSendRTMpmDataMessageHandler.java:526)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at android.os.Looper.loop(Looper.java:137)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at android.app.ActivityThread.main(ActivityThread.java:4921)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at java.lang.reflect.Method.invokeNative(Native Method)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at java.lang.reflect.Method.invoke(Method.java:511)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-12 12:48:52.993: E/AndroidRuntime(15260): 	at dalvik.system.NativeStart.main(Native Method)
08-12 12:48:53.013: E/android.os.Debug(2224): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
08-12 12:48:53.018: I/dumpstate(308): begin





1.什么是 OutOfMemoryError:

   官方引用: Thrown when a request for memory is made that can not be satisfied using the available platform resources. Such a request may be made by both the running application or by an internal function of the VM.
   通俗的讲:就是在请求一块内存的时候,当前可用资源不够用来请求时抛出的一种错误。我们知道,每个 android 程序就是一个独立 dalvik vm 实例,每个实例限制了最大内存占用,如果超过了这个限制,系统就会抛出这个错误。所以跟整个设备的剩余内存没太大关系,当然如果设备剩余内存都不足以再运行一个程序时,系统就会选择 kill 部分程序以确保有足够内存运行其他程序。

 

2.android 内存组成:

   android 内存由 dalvik 和 native 2部分组成,dalvik 也就是 java 堆,创建的对象就是在这里分配的,而 native 是通过 c/c++ 方式申请的内存,Bitmap 就是以一种方式分配的(android3.0 以后,系统默认是通过 dalvik 分配的)。当然无论以何种方式分配,2部分加起来不能超过 android 对单个程序的内存限制。

 

3.内存限制大小:

1 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
2 activityManager.getMemoryClass();

 以上方法会返回以 M 为单位的数字,可能在不同的平台或者设备上值都不太一样,比如:HTC G7 默认 24M,Galaxy 36M,emulator-2.3 24M,等等。

 

4.程序实际占用:

  以一个简单的 android 程序为例,该程序是用 eclipse adt 自动生成的最简单的一个 android 项目,只有1个 activity 和 adt 自动生成的 res 目录,测试环境:emulator-2.3.3
  启动该程序,命令行运行:

 

adb shell dumpsys meminfo com.mem.demo

 

  执行结果:

 

复制代码
Applications Memory Usage (kB):

Uptime: 1195344 Realtime: 1195344



** MEMINFO in pid 333 [com.mem.demo] **

                    native   dalvik    other    total
    --------------------------------------------------------
    |       size:     3968     5379      N/A     9347      |
    |                                                      |
    |   allocated:    3964     2649      N/A     6613      |
    --------------------------------------------------------
            free:        3     2730      N/A     2733

           (Pss):      553      449     2516     3518

  (shared dirty):     2272     1868     6648    10788

    (priv dirty):      420       32     1140     1592

 

 Objects

           Views:        0        ViewRoots:        0

     AppContexts:        0       Activities:        0

          Assets:        2    AssetManagers:        2

   Local Binders:        5    Proxy Binders:       10

Death Recipients:        0

 OpenSSL Sockets:        0

 

 SQL

               heap:        0         MEMORY_USED:        0

 PAGECACHE_OVERFLOW:        0         MALLOC_SIZE:        0

 

 

 Asset Allocations

    zip:/data/app/com.mem.demo-1.apk:/resources.arsc: 1K
复制代码

 

从上面被框出来的部分可以看出,一个最简单的 android 程序在启动后都有 6m 左右内存的占用(上面是 6613kb)。那这 6m 的内存除了该 android 自己的资源和类之外,其他的还有什么呢:

简单说:在初始化的时候会 preload 一些东西,这些就包括 classes 和系统资源,就是系统的一些布局啊,图片啊,等等,在 android 完成启动以后,这部分就通过内存共享的方式共享给其他程序,可以让其他程序可以调用这部分资源,代码可以参考:http://goo.gl/EKvCV,android 整个启动流程可以参考:http://goo.gl/K36Lr 。

 

5.发生 OOM :

 为了制造 OOM,我们对上面最简单的程序进行了改写:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  com.mem.demo;
 
import  android.app.Activity;
import  android.graphics.Bitmap;
import  android.graphics.BitmapFactory;
import  android.os.Bundle;
 
public  class  DemoActivity extends  Activity {
     Bitmap map1, map2, map3, map4;
 
     /** Called when the activity is first created. */
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
 
         map1 = BitmapFactory.decodeResource(getResources(), R.drawable.big1);
         map2 = BitmapFactory.decodeResource(getResources(), R.drawable.big2);
         map3 = BitmapFactory.decodeResource(getResources(), R.drawable.big3);
         map4 = BitmapFactory.decodeResource(getResources(), R.drawable.big4);
     }
}

 

其中:big1 到 big4 都是 1600*900 分辨率的图片,放在 drawbable-hdpi 文件夹下面,启动程序,发生了 OOM:

 

复制代码
05-08 07:44:44.372: E/dalvikvm-heap(386): 5760000-byte external allocation too large for this process.
05-08 07:44:44.412: I/dalvikvm-heap(386): Clamp target GC heap from 25.099MB to 24.000MB
05-08 07:44:44.412: E/GraphicsJNI(386): VM won't let us allocate 5760000 bytes
05-08 07:44:44.412: D/dalvikvm(386): GC_FOR_MALLOC freed 0K, 53% free 2548K/5379K, external 18500K/20548K, paused 36ms
05-08 07:44:44.422: D/skia(386): --- decoder->decode returned false
05-08 07:44:44.422: D/AndroidRuntime(386): Shutting down VM
05-08 07:44:44.432: W/dalvikvm(386): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-08 07:44:44.442: E/AndroidRuntime(386): FATAL EXCEPTION: main
05-08 07:44:44.442: E/AndroidRuntime(386): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
05-08 07:44:44.442: E/AndroidRuntime(386):     at com.mem.demo.DemoActivity.onCreate(DemoActivity.java:20)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.os.Looper.loop(Looper.java:123)
05-08 07:44:44.442: E/AndroidRuntime(386):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-08 07:44:44.442: E/AndroidRuntime(386):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 07:44:44.442: E/AndroidRuntime(386):     at java.lang.reflect.Method.invoke(Method.java:507)
05-08 07:44:44.442: E/AndroidRuntime(386):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 07:44:44.442: E/AndroidRuntime(386):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 07:44:44.442: E/AndroidRuntime(386):     at dalvik.system.NativeStart.main(Native Method)
复制代码

 

从日志上看,内存不足发生在 decode big4 这个图片的时候,我们对内存情况进行打印:

 

复制代码
Applications Memory Usage (kB):
Uptime: 958383 Realtime: 958383

** MEMINFO in pid 386 [com.mem.demo] **
                    native   dalvik    other    total
            size:    25144     5379      N/A    30523
       allocated:    20799     2614      N/A    23413
            free:       60     2765      N/A     2825
           (Pss):      494      426    18494    19414
  (shared dirty):     2288     1876     5292     9456
    (priv dirty):      360       28    17836    18224
复制代码

 

从内存打印情况看出,当前已经分配了 23m 左右内存,同时结合 logcat 错误日志,big4 这个图片需要申请 5760000byte,即:5.76m 的内存,加起来就是 29m 左右内存,我们测试环境是 emulator2.3.3 默认是
24m,内存不足以申请这么多,所以抛出了OOM。

那为什么区区3,4张图片就会让 android 程序内存不足? 
设备限制是一方面,像上面第3点说的,每个 android 设备的内存限制不一样,这个程序在模拟器上会有问题,在其他设备上,比如:galaxy 就不会有问题。最主要的还是跟图片所占内存有关系,那么一张图片到底
占用多少内存呢,java 没有 c 的 sizeof() 函数,无法准确去量化这个数值,但是可以有粗略的计算方法:

 

宽 * 高 * 每个像素所占的 bytes

 

宽度和高度这个很容易获得,那每个像素所占的 bytes 呢,这个主要取决于 decode 图片的方式:

 

复制代码
Bitmap.Config     ALPHA_8        Each pixel is stored as a single translucency (alpha) channel. 
Bitmap.Config     ARGB_4444      This field is deprecated. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead.  
Bitmap.Config     ARGB_8888      Each pixel is stored on 4 bytes. 
Bitmap.Config     RGB_565        Each pixel is stored on 2 bytes and only the RGB channels are encoded: red is stored with 5 bits of precision (32 possible values), green is store                                 d with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision. 
复制代码

 

以上是官方文档对 Bitmap.Config 类的描述,所以,如果以 ARGB_8888 的方式 decode,那个每个像素占用4个 bytes,而如果用 RGB_565 就占用2个 bytes。
我们计算一下,在 2.3 以后,程序自带的图片资源,都默认以 ARGB_8888 的方式,而在此以之前是以 RGB_565 的方式(不确定,待验证),所以颜色会有损耗,典型的就是如果有渐变色的话,会出现光圈。
所以,计算如下:

 

1600 * 900 * 4 = 5760000

 

 这个 5760000 也就是上面 logcat 错误日志里面所提到的申请数字,当然在实际用命令打印出的内存情况上看,比这个数字要大,是因为这只是图片像素的内存,还有一些属性,变量和类本身没有计算在内。




你可能感兴趣的:(Android 开发)