Android -- Drawable and Bitmap

Source: Internet
Author: User

Android -- Drawable and Bitmap
Below is the code to test the loading of 1000 Drawable objects: copy the code 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 ("", "test No." + (I + 1) + "image"); array [I] = getResources (). getDrawable (R. drawable. img );} Copy the code output result: copy the code 04-07 21:49:25. 248: D/szipinf (7828): Initializing inflate state04-07 21:49:25. 398: E/(7828): test 1st images 04-07 21:49:25. 658: D/dalvikvm (7828): GC_EXTERNAL_ALLOC freed 48 K, 50% free 2692 K/5379 K, external 0 K/0 K, paused 24ms04-07 21:49:25. 748: E/(7828): Test 2nd images 04-07 21:49:25. 748: E/(7828): Test 3rd images .................................... 21:49:26 04-07. 089: E/(7828): Test 998th images 04-07 21:49:26. 089: E/(7828): Test 999th images 04-07 21:49:26. 089: E/(7828): The Code Program for copying 1,000th images does not report an error and runs normally. It is okay to load 1000 Drawable objects. Next, let's take a look at the code for loading 1000 Bitmap objects: copy the code public class Main extends Activity {int number = 1000; 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 ("", "test No." + (I + 1) + "image"); bitmap [I] = BitmapFactory. decodeResource (getResources (), R. drawab Le. img) ;}} copy the code output result: copy the code 04-07 22:06:05. 344: D/szipinf (7937): Initializing inflate state04-07 22:06:05. 374: E/(7937): test 1st images 04-07 22:06:05. 544: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed 51 K, 50% free 2692 K/5379 K, external 0 K/0 K, paused 40ms04-07 22:06:05. 664: E/(7937): Test 2nd images 04-07 22:06:05. 774: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed 1 K, 50% free 2691 K/5379 K, external 6026 K/7525 K, paused 31 m S04-07 22:06:05. 834: E/(7937): Test 3rd images 04-07 22:06:05. 934: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 12052 K/14100 K, paused 24ms04-07 22:06:06. 004: E/(7937): Test 4th images 04-07 22:06:06. 124: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 18078 K/20126 K, paused 27ms04-07 22:06:06. 204: E/(7937): Test 5th images 04-07 22:06:06. 315: D/dalvikvm (7937): GC _ EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 24104 K/26152 K, paused 26ms04-07 22:06:06. 395: E/(7937): Test 6th images 04-07 22:06:06. 495: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 30130 K/32178 K, paused 22ms04-07 22:06:06. 565: E/(7937): Test 7th images 04-07 22:06:06. 665: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 36156 K/38204 K, pause D 22ms04-07 22:06:06. 745: E/(7937): Test 8th images 04-07 22:06:06. 845: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed 2 K, 51% free 2689 K/5379 K, external 42182 K/44230 K, paused 23ms04-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.239 MB to 48.000MB04-07 22:06:06. 885: E/GraphicsJNI (7937): V M won't let us allocate 6170724 bytes04-07 22:06:06. 885: D/dalvikvm (7937): GC_FOR_MALLOC freed <1 K, 51% free 2689 K/5379 K, external 42182 K/44230 K, paused 25ms04-07 22:06:06. 885: D/AndroidRuntime (7937): Shutting down VM04-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: main04-07 22: 06: 06.885: E/AndroidRuntime (7937): java. lang. outOfMemoryError: bitmap size exceeds VM budget04-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) 22:06:06 04-07. 885: E/AndroidR Untime (7937): at android. app. instrumentation. callActivityOnCreate (Instrumentation. java: 1047) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. app. activityThread. extends mlaunchactivity (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. activity Thread. 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. logoff. loop (logoff. 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/AndroidRuntim (E (7937): at com. android. internal. OS. zygoteInit. main (ZygoteInit. java: 605) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at dalvik. system. nativeStart. the main (Native Method) copies the code and looks at the above output to load the 8th images. Then the program reports the error "java. lang. outOfMemoryError: bitmap size exceeds VM budget ". The preceding example shows that using Drawable to save image objects occupies less memory. The use of Biamtp objects will occupy a lot of memory space and it is easy to see OOM! Next let's take a look at an example. This is also the loading of Bitmap objects. However, instead of using the decodeResource method of BitmapFactory, decodeStream is used to view the code. Copy the 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 ("", "test No." + (I + 1) + "image"); bitmap [I] = BitmapFactory. decodeStream (getResources (). openRawResource (R. drawable. img); // method changed here }}} Copy the code output result: copy the code 04-07 22:16:12. 676: E/(8091): Test 561st images 04-07 22:16:12. 756: E/(8091): Test 562nd images 04-07 22:16:12. 826: E/(8091): Test 563rd images 04-07 22:16:12. 906: E/(8091): Test 564th images 04-07 22:16:12. 906: D/skia (8091): ---------- mmap failed for imageref_ashmem size = 2744320 err = 1204-07 22:16:12. 906: E/(8091): Test 565th images 04-07 22:16:12. 906: D/skia (8091): ---------- mmap failed for imageref_ashmem size = 2744320 err = 1 204-07 22:16:12. 906: E/(8091): Test 566th images 04-07 22:16:12. 916: E/filemap (8091): mmap (416798) failed: Out of memory04-07 22:16:12. 916: D/filemap (8091): munmap (0x0, 0) failed04-07 22:16:12. 916: W/asset (8091): create map from entry failed04-07 22:16:12. 916: D/AndroidRuntime (8091): Shutting down VM04-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: main04-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 #0x7f02000104-07 22:16:12. 936: E/AndroidRuntime (8091): at android. app. ActivityThread. extends mlaunchactivity (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. jav A: 939) 22:16:12 04-07. 936: E/AndroidRuntime (8091): at android. OS. handler. dispatchMessage (Handler. java: 99) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. OS. logoff. loop (logoff. 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 2 2: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 (N Ative 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 #0x7f02000104-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) 22:16:12 04-07. 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. extends mlaunchactivity (ActivityThread. java: 1722) 04-07 22:16:12. 936: E/AndroidRuntime (8091 ):... 11 mor E04-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. conte Nt. res. resources. openRawResource (Resources. java: 857) 04-07 22:16:12. 936: E/AndroidRuntime (8091 ):... 15 more copy code from the above we can see that the OOM error occurs when the program loads 566th sheets. However, compared with the 2nd examples, you will find that the program can load more images. This indicates that the decodeResource method using BitmapFactory occupies a large amount of memory, while the decodeStream method occupies a smaller memory. As shown in the above three examples, I can use the first method (that is, using Drawable to load images) to load more images, it takes about 0.01 seconds to load 32 images! I tried to adjust the number of Drawable to 1000000. When the program is running, it stops in the 153761 images. The mobile phone prompts, "the application has no response ..." I guess that Drawable should not belong to the resident memory object. Otherwise, it is impossible to avoid OOM ~~ I did another test just now, and set array [I] = getResources () in the first example (). getDrawable (R. drawable. img); the method is replaced by array [I] = Drawable. createFromStream (getResources (). openRawResource (R. drawable. img), null); the result is the same as that in the third example. An OOM error occurs in the 566th images! In addition, the loading time is the same ~~ I am the tianguan Tiao's Tipp line Bitmap to DrawableBitmap bm = xxx; // xxx obtain BitmapDrawable bd = BitmapDrawable (bm) based on your situation ); android Development Network prompts that BtimapDrawable is a subclass of Drawable, And you can directly use the bd object. After Drawable is converted to Bitmap to a Bitmap object, the Drawable object can be converted into a byte output stream through the Android SK inventory, and finally saved as jpg and png files. Drawable d = xxx; // xxx obtain drawableBitmapDrawable bd = (BitmapDrawable) d according to your own situation; Bitmap bm = bd. getBitmap (); finally, bm is the Bitmap object we need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.