Drawable and bitmap

Source: Internet
Author: User
Tags skia

Drawable

The following code is used to test the loading of 1000 drawable objects:

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 );}}}

Output result:

21:49:25 04-07. 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): Test 1,000th Images

The program does not report an error and runs normally. It is okay to load 1000 drawable objects.

Bitmap

Next let's take a look at the code for loading 1000 bitmap objects:

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. decoderesource (getresources (), R. drawable. IMG );}}}

Output result:

22:06:05 04-07. 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 31ms04-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, paused 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): VM 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/androidruntime (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. 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. 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/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)

Check the above output to load 8th images. 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.

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 }}}

Output result:

22:16:12 04-07. 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 = 1204-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. 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. 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 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 #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 more04-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

We can see from the above 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.

I am the dividing line of tiantiao

From the above three examples, we can see that using the first method (using drawable to load images) can load more images, and loading 32 images takes about 0.01 seconds! 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.

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

The method is changed

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 dividing line of tiantiao

  • Bitmap to drawable
Bitmap 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.

  • Drawable to bitmap

After being converted 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.

 

 

 

 

 

Reference: http://blog.csdn.net/ygc973797893/article/details/7620352

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.