Drawable
以下這個是測試加載1000個Drawable對象的代碼:
復制代碼
public class Main extends Activity
{
int number = 1000;
Drawable[] array;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array = new BitmapDrawable[number];
for(int i = 0; i < 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-07 21:49:26.089: E/(7828): 測試第998張圖片
04-07 21:49:26.089: E/(7828): 測試第999張圖片
04-07 21:49:26.089: E/(7828): 測試第1000張圖片
復制代碼
程序沒有報錯,正常運行,加載1000個Drawable對象沒問題。
Bitmap
下面再來看一下加載1000個Bitmap對象的代碼:
復制代碼
public class Main extends Activity
{
int number = 1000;
Bitmap bitmap[];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bitmap = new Bitmap[number];
for (int i = 0; i < 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, external 6026K/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, external 12052K/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, external 18078K/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, external 24104K/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, external 30130K/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, external 36156K/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, external 42182K/44230K, paused 23ms
04-07 22:06:06.845: E/dalvikvm-heap(7937): 6170724-byte external allocation too large for this process.
04-07 22:06:06.885: I/dalvikvm-heap(7937): Clamp target GC heap from 48.239MB to 48.000MB
04-07 22:06:06.885: E/GraphicsJNI(7937): VM won't let us allocate 6170724 bytes
04-07 22:06:06.885: D/dalvikvm(7937): GC_FOR_MALLOC freed <1K, 51% free 2689K/5379K, external 42182K/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 size exceeds VM budget
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.nativeCreate(Native Method)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
04-07 22:06:06.885: E/AndroidRuntime(7937): at bassy.test.drawable.Main.onCreate(Main.java:37)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.os.Looper.loop(Looper.java:130)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread.main(ActivityThread.java:3835)
04-07 22:06:06.885: E/AndroidRuntime(7937): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:06:06.885: E/AndroidRuntime(7937): at java.lang.reflect.Method.invoke(Method.java:507)
04-07 22:06:06.885: E/AndroidRuntime(7937): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-07 22:06:06.885: E/AndroidRuntime(7937): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-07 22:06:06.885: E/AndroidRuntime(7937): at dalvik.system.NativeStart.main(Native Method)
復制代碼
看看上面的輸出,才加載到第8張圖片,程序就報錯了“java.lang.OutOfMemoryError: bitmap size exceeds VM budget”。通過上面的例子,可以看清楚地看出來,使用Drawable保存圖片對象,占用更小的內存空間。而使用Biamtp對象,則會占用很大內存空間,很容易就出現OOM了!
下面我們再來看一個例子,這個也是加載Bitmap對象。只不過,之次不是使用BitmapFactory的decodeResource方法,而是使用decodeStream方法,看代碼。
復制代碼
public class Main extends Activity
{
int number = 1000;
Bitmap bitmap[];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bitmap = new Bitmap[number];
for (int i = 0; i < 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=2744320 err=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=2744320 err=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 start activity ComponentInfo{bassy.test.drawable/bassy.test.drawable.Main}: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f020001
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.os.Looper.loop(Looper.java:130)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.ActivityThread.main(ActivityThread.java:3835)
04-07 22:16:12.936: E/AndroidRuntime(8091): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:16:12.936: E/AndroidRuntime(8091): at java.lang.reflect.Method.invoke(Method.java:507)
04-07 22:16:12.936: E/AndroidRuntime(8091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-07 22:16:12.936: E/AndroidRuntime(8091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-07 22:16:12.936: E/AndroidRuntime(8091): at dalvik.system.NativeStart.main(Native Method)
04-07 22:16:12.936: E/AndroidRuntime(8091): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f020001
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.content.res.Resources.openRawResource(Resources.java:860)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.content.res.Resources.openRawResource(Resources.java:836)
04-07 22:16:12.936: E/AndroidRuntime(8091): at bassy.test.drawable.Main.onCreate(Main.java:43)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-07 22:16:12.936: E/AndroidRuntime(8091): ... 11 more
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): at android.content.res.AssetManager.openNonAssetNative(Native Method)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.content.res.AssetManager.openNonAsset(AssetManager.java:429)
04-07 22:16:12.936: E/AndroidRuntime(8091): at android.content.res.Resources.openRawResource(Resources.java:857)
04-07 22:16:12.936: E/AndroidRuntime(8091): ... 15 more
復制代碼
從上面可以看出,程序在加載到第566張的時候,就出現了OOM錯誤。不過,跟第2個例子比起來,你會發現,程序可以加載更多的圖片。這說明了使用BitmapFactory的decodeResource方法會占據大量內存,而使用使用decodeStream方法,則占據更小的內存。
我是天王蓋地虎的分割線
從上面三個例子,可以看出,用第一種方法(即用Drawable加載圖片)可以加載更加的圖片,加載32張圖片的時間約為0.01秒!我試著把Drawable的數量調至1000000,程序在運行時,停在了153761張圖片裡,手機提示,“應用程序無響應…”
個人猜測,Drawable應該不屬於常駐內存的對象,不然的話,不可能不會出現OOM的~~
剛才又做了個測試,把第一個例子中的
array[i] = getResources().getDrawable(R.drawable.img);
方法換成了
array[i] = Drawable.createFromStream(getResources().openRawResource(R.drawable.img), null);
結果和第三個例子一樣,在第566張圖片中,出現了OOM錯誤!而且,加載的時間都是一樣~~
我是天王蓋地虎的分割線
Bitmap轉Drawable
Bitmap bm=xxx; //xxx根據你的情況獲取
BitmapDrawable bd=BitmapDrawable(bm);
Android開發網提示因為BtimapDrawable是Drawable的子類,最終直接使用bd對象即可。
Drawable轉Bitmap
轉成Bitmap對象後,可以將Drawable對象通過Android的SK庫存成一個字節輸出流,最終還可以保存成為jpg和png的文件。
Drawable d=xxx; //xxx根據自己的情況獲取drawable
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();
最終bm就是我們需要的Bitmap對象了。