Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. public class ImageLoader {
  2.  
  3. //the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
  4. private HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();
  5.  
  6. private File cacheDir;
  7.  
  8. public ImageLoader(Context context){
  9. //Make the background thead low priority. This way it will not affect the UI performance
  10. photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
  11.  
  12. //Find the dir to save cached images
  13. if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
  14. cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
  15. else
  16. cacheDir=context.getCacheDir();
  17. if(!cacheDir.exists())
  18. cacheDir.mkdirs();
  19. }
  20.  
  21. final int stub_id=R.drawable.stub;
  22. public void DisplayImage(String url, Activity activity, ImageView imageView)
  23. {
  24. if(cache.containsKey(url))
  25. imageView.setImageBitmap(cache.get(url));
  26. else
  27. {
  28. queuePhoto(url, activity, imageView);
  29. imageView.setImageResource(stub_id);
  30. }
  31. }
  32.  
  33. private void queuePhoto(String url, Activity activity, ImageView imageView)
  34. {
  35. //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them.
  36. photosQueue.Clean(imageView);
  37. PhotoToLoad p=new PhotoToLoad(url, imageView);
  38. synchronized(photosQueue.photosToLoad){
  39. photosQueue.photosToLoad.push(p);
  40. photosQueue.photosToLoad.notifyAll();
  41. }
  42.  
  43. //start thread if it's not started yet
  44. if(photoLoaderThread.getState()==Thread.State.NEW)
  45. photoLoaderThread.start();
  46. }
  47.  
  48. private Bitmap getBitmap(String url)
  49. {
  50. //I identify images by hashcode. Not a perfect solution, good for the demo.
  51. String filename=String.valueOf(url.hashCode());
  52. File f=new File(cacheDir, filename);
  53.  
  54. //from SD cache
  55. Bitmap b = decodeFile(f);
  56. if(b!=null)
  57. return b;
  58.  
  59. //from web
  60. try {
  61. Bitmap bitmap=null;
  62. InputStream is=new URL(url).openStream();
  63. OutputStream os = new FileOutputStream(f);
  64. Utils.CopyStream(is, os);
  65. os.close();
  66. bitmap = decodeFile(f);
  67. return bitmap;
  68. } catch (Exception ex){
  69. ex.printStackTrace();
  70. return null;
  71. }
  72. }
  73.  
  74. //decodes image and scales it to reduce memory consumption
  75. private Bitmap decodeFile(File f){
  76. try {
  77. //decode image size
  78. BitmapFactory.Options o = new BitmapFactory.Options();
  79. o.inJustDecodeBounds = true;
  80. BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  81.  
  82. //Find the correct scale value. It should be the power of 2.
  83. final int REQUIRED_SIZE=70;
  84. int width_tmp=o.outWidth, height_tmp=o.outHeight;
  85. int scale=1;
  86. while(true){
  87. if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
  88. break;
  89. width_tmp/=2;
  90. height_tmp/=2;
  91. scale++;
  92. }
  93.  
  94. //decode with inSampleSize
  95. BitmapFactory.Options o2 = new BitmapFactory.Options();
  96. o2.inSampleSize=scale;
  97. return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  98. } catch (FileNotFoundException e) {}
  99. return null;
  100. }
  101.  
  102. //Task for the queue
  103. private class PhotoToLoad
  104. {
  105. public String url;
  106. public ImageView imageView;
  107. public PhotoToLoad(String u, ImageView i){
  108. url=u;
  109. imageView=i;
  110. }
  111. }
  112.  
  113. PhotosQueue photosQueue=new PhotosQueue();
  114.  
  115. public void stopThread()
  116. {
  117. photoLoaderThread.interrupt();
  118. }
  119.  
  120. //stores list of photos to download
  121. class PhotosQueue
  122. {
  123. private Stack<PhotoToLoad> photosToLoad=new Stack<PhotoToLoad>();
  124.  
  125. //removes all instances of this ImageView
  126. public void Clean(ImageView image)
  127. {
  128. for(int j=0 ;j<photosToLoad.size();){
  129. if(photosToLoad.get(j).imageView==image)
  130. photosToLoad.remove(j);
  131. else
  132. ++j;
  133. }
  134. }
  135. }
  136.  
  137. class PhotosLoader extends Thread {
  138. public void run() {
  139. try {
  140. while(true)
  141. {
  142. //thread waits until there are any images to load in the queue
  143. if(photosQueue.photosToLoad.size()==0)
  144. synchronized(photosQueue.photosToLoad){
  145. photosQueue.photosToLoad.wait();
  146. }
  147. if(photosQueue.photosToLoad.size()!=0)
  148. {
  149. PhotoToLoad photoToLoad;
  150. synchronized(photosQueue.photosToLoad){
  151. photoToLoad=photosQueue.photosToLoad.pop();
  152. }
  153. Bitmap bmp=getBitmap(photoToLoad.url);
  154. cache.put(photoToLoad.url, bmp);
  155. if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){
  156.  
  157. BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
  158. System.out.println("Activity is"+photoToLoad.imageView.getContext());
  159. Activity a=(Activity)photoToLoad.imageView.getContext();
  160. a.runOnUiThread(bd);
  161. }
  162. }
  163. if(Thread.interrupted())
  164. break;
  165. }
  166. } catch (InterruptedException e) {
  167. //allow thread to exit
  168. }
  169. }
  170. }
  171.  
  172. PhotosLoader photoLoaderThread=new PhotosLoader();
  173.  
  174. //Used to display bitmap in the UI thread
  175. class BitmapDisplayer implements Runnable
  176. {
  177. Bitmap bitmap;
  178. ImageView imageView;
  179. public BitmapDisplayer(Bitmap b, ImageView i){bitmap=b;imageView=i;}
  180. public void run()
  181. {
  182. if(bitmap!=null)
  183. imageView.setImageBitmap(bitmap);
  184. else
  185. imageView.setImageResource(stub_id);
  186. }
  187. }
  188.  
  189. public void clearCache() {
  190. //clear memory cache
  191. cache.clear();
  192.  
  193. //clear SD cache
  194. File[] files=cacheDir.listFiles();
  195. for(File f:files)
  196. f.delete();
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement