Android loads more images

Source: Internet
Author: User

This was after the improvement yesterday. I tested it and can load 50 thousand images. It is estimated that more than 50 thousand images are OK. I only tried 50 thousand images, but it is not necessary to do this, in reality, about 1000 is almost the same, but my application is almost the same as 100, package com. lanlong. test; import java. io. file; import java. lang. ref. softReference; import java. lang. ref. weakReference; import java. util. hashMap; import java. util. map; import android. app. activity; import android. graphics. bitmap; 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 = 50000; Drawable [] array; Bitmap [] array2; private Map <String, SoftReference <Drawable> imageCache = new HashMap <String, softReference <Drawable> (); 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]; // same as BitmapDrawable // array2 = new Bitmap [number]; file mfile = new File (iconPath); Log. I ("", "mfile. exists () = "+ mfile. exists (); Drawable draw = null; long timeBegin = System. currentTimeMillis (); for (int I = 0; I <number; I ++) {Log. e ("", "test No." + (I + 1) + "picture"); // time = 2 second 10000 sheets 8 second 50000 sheets less than 1 second, 29 ms 100 sheets // 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); // max221 // array [I] = BitmapDrawable. createFromPath (iconPath); // max 221 // time = 149 second, 10000; 1 second, 100; 901 second, 50000 addDrawableToCache ("" + I, iconPath ); draw = getDrawableByPath ("" + I, iconPath); // array [I] = getBitmapByPath ("" + I, iconPath); Log. I ("", "draw =" + draw); 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} long timeEnd = System. currentTimeMillis (); Log. v ("", "time =" + (timeEnd-timeBegin)/1000 + "second");} public void addDrawableToCache (String id, String path) {// strongly referenced Bitmap object Drawable drawable = Drawable. createFromPath (path); // soft-referenced Bitmap object SoftReference <Drawable> softDrawable = new SoftReference <Drawable> (drawable); // Add this object to Map to cache imageCache. put (id, softDrawable); Log. w ("", "add, imageCache. size () = "+ imageCache. size ();} public Drawable getDrawableByPath (String id, String path) {// obtain soft referenced Bitmap object SoftReference <Drawable> softDrawable = imageCache from the cache. get (id); // determines whether soft reference if (softDrawable = null) {return null;} // retrieves a Bitmap object. if Bitmap is recycled due to insufficient memory, empty Drawable drable = softDrawable will be obtained. get (); return drable ;}}

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.