首页 > 移动开发 > Drawable和Bitmap在内存中谁更省内存

Drawable和Bitmap在内存中谁更省内存

今天在网上查找了下相关的资料,确实很少这方面的,不过也找到了一篇,根据这篇也做了相关测试

发现除了getResources().getDrawable(R.drawable.ic_launcher)这个方法有绝对优势外,此方法可以加载图片到1000张,

array2[i] = BitmapFactory.decodeFile(iconPath);//max 222
array[i] = Drawable.createFromPath(iconPath);//max 221
array[i] = BitmapDrawable.createFromPath(iconPath);//max  221

读取文件的方法Drawable和Bitmap都差不多,都是只能到200张左右

用流的方法读取结果,

array[i] = BitmapDrawable.createFromStream(
//                        new FileInputStream(iconPath), iconPath);//max 111

//                array2[i] = BitmapFactory.decodeStream(
//                        new FileInputStream(iconPath));//max 221 ;

 

我测试的代码如下

package com.lanlong.test;

import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class AndrodTestActivity extends Activity {

int number = 1000;

Drawable[] array;
Bitmap [] array2;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String iconPath = Environment.getExternalStorageDirectory()
+"/vpn/Bafangtong/image/weixin.png";

array = new Drawable[number];//BitmapDrawable 一样

array2 = new Bitmap[number];
File mfile = new File(iconPath);
Log.i("","mfile.exists() = "+mfile.exists());

for(int i = 0; i < number; i++)

{

Log.e("", "测试第" + (i+1) + "张图片");

//            array[i] = getResources().getDrawable(R.drawable.ic_launcher);//1000 ok
//            array2[i] = BitmapFactory.decodeFile(iconPath);//max 222
//            zoomImg(array2[i], 800, 480);
//            array[i] = Drawable.createFromPath(iconPath);//max 221
array[i] = BitmapDrawable.createFromPath(iconPath);//max  221
try {

//                array[i] = Drawable.createFromStream(
//                        new FileInputStream(iconPath), iconPath);//max 111

//                array[i] = BitmapDrawable.createFromStream(
//                        new FileInputStream(iconPath), iconPath);//max 111

//                array2[i] = BitmapFactory.decodeStream(
//                        new FileInputStream(iconPath));//max 221 ;
Log.i("", "array["+i+"] = "+array[i]);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public  Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return newbm;
}
}

日志打印信息

08-28 10:15:27.009: E/(11960): 测试第211张图片
08-28 10:15:27.024: E/(11960): 测试第212张图片
08-28 10:15:27.034: E/(11960): 测试第213张图片
08-28 10:15:27.049: E/(11960): 测试第214张图片
08-28 10:15:27.064: E/(11960): 测试第215张图片
08-28 10:15:27.074: E/(11960): 测试第216张图片
08-28 10:15:27.104: E/(11960): 测试第217张图片
08-28 10:15:27.139: E/(11960): 测试第218张图片
08-28 10:15:27.159: E/(11960): 测试第219张图片
08-28 10:15:27.174: E/(11960): 测试第220张图片
08-28 10:15:27.189: E/(11960): 测试第221张图片
08-28 10:15:27.209: E/(11960): 测试第222张图片
08-28 10:15:27.249: I/dalvikvm-heap(11960): Clamp target GC heap from 70.389MB to 64.000MB
08-28 10:15:27.249: I/dalvikvm-heap(11960): Forcing collection of SoftReferences for 281616-byte allocation
08-28 10:15:27.314: I/dalvikvm-heap(11960): Clamp target GC heap from 70.381MB to 64.000MB
08-28 10:15:27.314: E/dalvikvm-heap(11960): Out of memory on a 281616-byte allocation.
08-28 10:15:27.314: I/dalvikvm(11960): "main" prio=5 tid=1 RUNNABLE
08-28 10:15:27.314: I/dalvikvm(11960):   | group="main" sCount=0 dsCount=0 obj=0x41f99538 self=0x40b06010
08-28 10:15:27.314: I/dalvikvm(11960):   | sysTid=11960 nice=0 sched=0/0 cgrp=apps handle=1074563116
08-28 10:15:27.314: I/dalvikvm(11960):   | schedstat=( 3654714000 557222000 1624 ) utm=311 stm=54 core=3
08-28 10:15:27.314: I/dalvikvm(11960):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-28 10:15:27.329: I/dalvikvm(11960):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658)
08-28 10:15:27.329: I/dalvikvm(11960):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:347)
08-28 10:15:27.329: I/dalvikvm(11960):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:430)
08-28 10:15:27.329: I/dalvikvm(11960):   at com.lanlong.test.AndrodTestActivity.onCreate(AndrodTestActivity.java:42)
08-28 10:15:27.329: I/dalvikvm(11960):   at android.app.Activity.performCreate(Activity.java:5066)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.os.Looper.loop(Looper.java:155)
08-28 10:15:27.354: I/dalvikvm(11960):   at android.app.ActivityThread.main(ActivityThread.java:5485)
08-28 10:15:27.354: I/dalvikvm(11960):   at java.lang.reflect.Method.invokeNative(Native Method)
08-28 10:15:27.354: I/dalvikvm(11960):   at java.lang.reflect.Method.invoke(Method.java:511)
08-28 10:15:27.354: I/dalvikvm(11960):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-28 10:15:27.354: I/dalvikvm(11960):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-28 10:15:27.354: I/dalvikvm(11960):   at dalvik.system.NativeStart.main(Native Method)
08-28 10:15:27.354: E/dalvikvm(11960): Out of memory: Heap Size=65571KB, Allocated=63680KB, Limit=65536KB
08-28 10:15:27.354: E/dalvikvm(11960): Extra info: Footprint=65571KB, Allowed Footprint=65571KB, Trimmed=0KB
08-28 10:15:27.354: D/skia(11960): --- decoder->decode returned false
08-28 10:15:27.354: W/dalvikvm(11960): threadid=1: thread exiting with uncaught exception (group=0x41f982d0)
08-28 10:15:27.359: E/AndroidRuntime(11960): FATAL EXCEPTION: main
08-28 10:15:27.359: E/AndroidRuntime(11960): java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=63680KB)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:347)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:430)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at com.lanlong.test.AndrodTestActivity.onCreate(AndrodTestActivity.java:42)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.Activity.performCreate(Activity.java:5066)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.os.Looper.loop(Looper.java:155)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at android.app.ActivityThread.main(ActivityThread.java:5485)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at java.lang.reflect.Method.invokeNative(Native Method)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at java.lang.reflect.Method.invoke(Method.java:511)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-28 10:15:27.359: E/AndroidRuntime(11960):     at dalvik.system.NativeStart.main(Native Method)

=======================================================================

下面这个是从网上找到的,地址http://blog.sina.com.cn/s/blog_5fc933730101ay5x.html

 

1, 比较Drawable与Bitmap占用内存大小

2, 比较BitmapFactory类的decodeResource方法与decodeStream方法的效率

好吧,先来看第1个测试!

以下这个是测试加载1000个Drawable对象的代码,很简单的,我就不解释了!

publicclassMainextendsActivity

{

   intnumber= 1000;

   Drawable[] array;

 

   @Override

   publicvoidonCreate(BundlesavedInstanceState)

   {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

 

       array = new BitmapDrawable[number];

                    

       for(int i = 0; i <</SPAN>number; i++)

       {

           Log.e("", "测试第" + (i+1) + "张图片");

           array[i] =getResources().getDrawable(R.drawable.img);

       }

   }

}

 

 

输出结果:

04-07 21:49:25.248: D/szipinf(7828):Initializing inflate state

04-07 21:49:25.398: E/(7828):测试第1张图片

04-07 21:49:25.658: D/dalvikvm(7828):GC_EXTERNAL_ALLOC freed 48K, 50% free 2692K/5379K, external 0K/0K,paused 24ms

04-07 21:49:25.748: E/(7828):测试第2张图片

04-07 21:49:25.748: E/(7828):测试第3张图片

………………

………………

04-0721:49:26.089: E/(7828): 测试第998张图片

04-0721:49:26.089: E/(7828): 测试第999张图片

04-0721:49:26.089: E/(7828): 测试第1000张图片

 

程序没有报错,正常运行,加载1000个Drawable对象没问题。

 

下面再来看一下加载1000个Bitmap对象的代码,同样的,代码很简单的,我就不解释了!

publicclassMainextendsActivity

{

   intnumber= 1000;

   Bitmap bitmap[];

 

   @Override

   publicvoidonCreate(BundlesavedInstanceState)

   {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

 

      bitmap= newBitmap[number];

 

      for(inti = 0; i<</SPAN> number;i++)

      {

          Log.e("", "测试第" + (i+1) +"张图片");

          bitmap[i] =BitmapFactory.decodeResource(getResources(),R.drawable.img);

      }

   }

}

 

输出结果:

04-07 22:06:05.344: D/szipinf(7937):Initializing inflate state

04-07 22:06:05.374: E/(7937):测试第1张图片

04-07 22:06:05.544: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed 51K, 50% free 2692K/5379K, external 0K/0K,paused 40ms

04-07 22:06:05.664: E/(7937):测试第2张图片

04-07 22:06:05.774: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed 1K, 50% free 2691K/5379K, external6026K/7525K, paused 31ms

04-07 22:06:05.834: E/(7937):测试第3张图片

04-07 22:06:05.934: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external12052K/14100K, paused 24ms

04-07 22:06:06.004: E/(7937):测试第4张图片

04-07 22:06:06.124: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external18078K/20126K, paused 27ms

04-07 22:06:06.204: E/(7937):测试第5张图片

04-07 22:06:06.315: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external24104K/26152K, paused 26ms

04-07 22:06:06.395: E/(7937):测试第6张图片

04-07 22:06:06.495: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external30130K/32178K, paused 22ms

04-07 22:06:06.565: E/(7937):测试第7张图片

04-07 22:06:06.665: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external36156K/38204K, paused 22ms

04-07 22:06:06.745: E/(7937):测试第8张图片

04-07 22:06:06.845: D/dalvikvm(7937):GC_EXTERNAL_ALLOC freed 2K, 51% free 2689K/5379K, external42182K/44230K, paused 23ms

04-07 22:06:06.845:E/dalvikvm-heap(7937): 6170724-byte external allocation too largefor this process.

04-07 22:06:06.885:I/dalvikvm-heap(7937): Clamp target GC heap from 48.239MB to48.000MB

04-07 22:06:06.885:E/GraphicsJNI(7937): VM won't let us allocate 6170724bytes

04-07 22:06:06.885: D/dalvikvm(7937):GC_FOR_MALLOC freed <1K, 51% free 2689K/5379K, external42182K/44230K, paused 25ms

04-07 22:06:06.885:D/AndroidRuntime(7937): Shutting down VM

04-07 22:06:06.885: W/dalvikvm(7937):threadid=1: thread exiting with uncaught exception(group=0x40015560)

04-07 22:06:06.885:E/AndroidRuntime(7937): FATAL EXCEPTION: main

04-07 22:06:06.885:E/AndroidRuntime(7937): java.lang.OutOfMemoryError: bitmap sizeexceeds VM budget

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.Bitmap.nativeCreate(NativeMethod)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.Bitmap.createBitmap(Bitmap.java:477)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.Bitmap.createBitmap(Bitmap.java:444)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atbassy.test.drawable.Main.onCreate(Main.java:37)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.app.ActivityThread.access$1500(ActivityThread.java:123)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:939)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.os.Handler.dispatchMessage(Handler.java:99)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.os.Looper.loop(Looper.java:130)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atandroid.app.ActivityThread.main(ActivityThread.java:3835)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atjava.lang.reflect.Method.invokeNative(NativeMethod)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atjava.lang.reflect.Method.invoke(Method.java:507)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)

04-07 22:06:06.885:E/AndroidRuntime(7937):   atdalvik.system.NativeStart.main(Native Method)

 

 

看看上面的输出,才加载到第8张图片,程序就报错了“java.lang.OutOfMemoryError: bitmap size exceeds VMbudget”。

 

通过上面的例子,可以看清楚地看出来,使用Drawable保存图片对象,占用更小的内存空间。

而使用Biamtp对象,则会占用很大内存空间,很容易就出现OOM了!

 

下面我们再来看一个例子,这个也是加载Bitmap对象。

只不过,之次不是使用BitmapFactory的decodeResource方法,

而是使用decodeStream方法,看代码。

public class Main extends Activity

{

   int number = 1000;

   Bitmap bitmap[];

 

   @Override

   public void onCreate(BundlesavedInstanceState)

   {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

 

      bitmap = new Bitmap[number];

 

      for (int i = 0; i <</SPAN>number; i++)

      {

          Log.e("", "测试第" + (i+1) + "张图片");

          bitmap[i] = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.img));//这里换了方法

      }

   }

}

 

 

输出结果:

04-07 22:16:12.676: E/(8091):测试第561张图片

04-07 22:16:12.756: E/(8091):测试第562张图片

04-07 22:16:12.826: E/(8091):测试第563张图片

04-07 22:16:12.906: E/(8091):测试第564张图片

04-07 22:16:12.906: D/skia(8091):---------- mmap failed for imageref_ashmem size=2744320err=12

04-07 22:16:12.906: E/(8091):测试第565张图片

04-07 22:16:12.906: D/skia(8091):---------- mmap failed for imageref_ashmem size=2744320err=12

04-07 22:16:12.906: E/(8091):测试第566张图片

04-07 22:16:12.916: E/filemap(8091):mmap(0,416798) failed: Out of memory

04-07 22:16:12.916: D/filemap(8091):munmap(0x0, 0) failed

04-07 22:16:12.916: W/asset(8091):create map from entry failed

04-07 22:16:12.916:D/AndroidRuntime(8091): Shutting down VM

04-07 22:16:12.916: W/dalvikvm(8091):threadid=1: thread exiting with uncaught exception(group=0x40015560)

04-07 22:16:12.936:E/AndroidRuntime(8091): FATAL EXCEPTION: main

04-07 22:16:12.936:E/AndroidRuntime(8091): java.lang.RuntimeException: Unable to startactivityComponentInfo{bassy.test.drawable/bassy.test.drawable.Main}:android.content.res.Resources$NotFoundException: Fileres/drawable-mdpi/img.png from drawable resource ID#0x7f020001

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.ActivityThread.access$1500(ActivityThread.java:123)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:939)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.os.Handler.dispatchMessage(Handler.java:99)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.os.Looper.loop(Looper.java:130)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.ActivityThread.main(ActivityThread.java:3835)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atjava.lang.reflect.Method.invokeNative(NativeMethod)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atjava.lang.reflect.Method.invoke(Method.java:507)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atdalvik.system.NativeStart.main(Native Method)

04-07 22:16:12.936:E/AndroidRuntime(8091): Caused by:android.content.res.Resources$NotFoundException: Fileres/drawable-mdpi/img.png from drawable resource ID#0x7f020001

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.content.res.Resources.openRawResource(Resources.java:860)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.content.res.Resources.openRawResource(Resources.java:836)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atbassy.test.drawable.Main.onCreate(Main.java:43)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)

04-07 22:16:12.936:E/AndroidRuntime(8091):   ... 11more

04-07 22:16:12.936:E/AndroidRuntime(8091): Caused by: java.io.FileNotFoundException:res/drawable-mdpi/img.png

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.content.res.AssetManager.openNonAssetNative(NativeMethod)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.content.res.AssetManager.openNonAsset(AssetManager.java:429)

04-07 22:16:12.936:E/AndroidRuntime(8091):   atandroid.content.res.Resources.openRawResource(Resources.java:857)

04-07 22:16:12.936:E/AndroidRuntime(8091):   ... 15more

 

从上面可以看出,程序在加载到第566张的时候,就出现了OOM错误。

不过,跟第2个例子比起来,你会发现,程序可以加载更多的图片。

这说明了使用BitmapFactory的decodeResource方法会占据大量内存,

而使用使用decodeStream方法,则占据更小的内存。

 

从时间上来说,看看日志输出,大概估算了一下加载一张图片所需要的时间,发现,

decodeResource加载图片需要约0.17秒的时间,

而使用decodeStream方法,只需要约0.08秒的时间!

这说明了,decodeStream无论是时间上还是空间上,都比decodeResource方法更优秀!!

 

 

从上面三个例子,可以看出,用第一种方法(即用Drawable加载图片)可以加载更加的图片,加载32张图片的时间约为0.01秒!

我试着把Drawable的数量调至1000000,程序在运行时,停在了153761张图片里,手机提示,“应用程序无响应…”

个人猜测,Drawable应该不属于常驻内存的对象,不然的话,不可能不会出现OOM的~~

 

网上关于Drawable与Bitmap的资料太少,不能深入学习,真是遗憾~

 

 

刚才又做了个测试,把第一个例子中的

array[i] =getResources().getDrawable(R.drawable.img);

方法换成了

array[i] =Drawable.createFromStream(getResources().openRawResource(R.drawable.img),null);


本文固定链接: http://www.devba.com/index.php/archives/2458.html | 开发吧

报歉!评论已关闭.