Guest User

Untitled

a guest
Apr 22nd, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. package com.samir;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import java.util.Collections;
  13. import java.util.Map;
  14. import java.util.WeakHashMap;
  15. import java.util.concurrent.ExecutorService;
  16. import java.util.concurrent.Executors;
  17.  
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.graphics.Bitmap;
  21. import android.graphics.BitmapFactory;
  22. import android.widget.ImageView;
  23.  
  24. public class ImageLoader {
  25.  
  26. MemoryCache memoryCache=new MemoryCache();
  27. FileCache fileCache;
  28. private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
  29. ExecutorService executorService;
  30.  
  31. public ImageLoader(Context context){
  32. fileCache=new FileCache(context);
  33. executorService=Executors.newFixedThreadPool(5);
  34. }
  35.  
  36. int stub_id = R.drawable.ic_launcher;
  37. public void DisplayImage(String url, ImageView imageView)
  38. {
  39.  
  40. // stub_id = loader;
  41. imageViews.put(imageView, url);
  42. Bitmap bitmap=memoryCache.get(url);
  43. if(bitmap!=null)
  44. imageView.setImageBitmap(bitmap);
  45. else
  46. {
  47. queuePhoto(url, imageView);
  48. imageView.setImageResource(stub_id);
  49. }
  50. }
  51.  
  52. private void queuePhoto(String url, ImageView imageView)
  53. {
  54. PhotoToLoad p=new PhotoToLoad(url, imageView);
  55. executorService.submit(new PhotosLoader(p));
  56. }
  57.  
  58. private Bitmap getBitmap(String url)
  59. {
  60. File f=fileCache.getFile(url);
  61.  
  62. //from SD cache
  63. Bitmap b = decodeFile(f);
  64. if(b!=null)
  65. return b;
  66.  
  67. //Download Images from the web
  68. try {
  69. Bitmap bitmap=null;
  70. URL imageUrl = new URL(url);
  71. HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
  72. conn.setConnectTimeout(30000);
  73. conn.setReadTimeout(30000);
  74. conn.setInstanceFollowRedirects(true);
  75. InputStream is=conn.getInputStream();
  76. OutputStream os = new FileOutputStream(f);
  77. Utils.CopyStream(is, os);
  78. os.close();
  79. // bitmap = decodeFile(f);
  80. bitmap=BitmapFactory.decodeStream(is);
  81. return bitmap;
  82. } catch (Throwable ex) {
  83. ex.printStackTrace();
  84. if (ex instanceof OutOfMemoryError)
  85. memoryCache.clear();
  86. return null;
  87. }
  88. }
  89.  
  90. //decodes image and scales it to reduce memory consumption
  91. private Bitmap decodeFile(File f){
  92. try {
  93. //decode image size
  94. BitmapFactory.Options o = new BitmapFactory.Options();
  95. o.inJustDecodeBounds = true;
  96. BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  97.  
  98. //Find the correct scale value. It should be the power of 2.
  99. final int REQUIRED_SIZE=300;
  100. int width_tmp=o.outWidth, height_tmp=o.outHeight;
  101. int scale=1;
  102. while(true){
  103. if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
  104. break;
  105. width_tmp/=2;
  106. height_tmp/=2;
  107. scale*=2;
  108. }
  109.  
  110. //decode with inSampleSize
  111. BitmapFactory.Options o2 = new BitmapFactory.Options();
  112. o2.inSampleSize=scale;
  113. return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  114. } catch (FileNotFoundException e) {
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. return null;
  119. }
  120.  
  121. //Task for the queue
  122. private class PhotoToLoad
  123. {
  124. public String url;
  125. public ImageView imageView;
  126. public PhotoToLoad(String u, ImageView i){
  127. url=u;
  128. imageView=i;
  129. }
  130. }
  131.  
  132. class PhotosLoader implements Runnable {
  133. PhotoToLoad photoToLoad;
  134. PhotosLoader(PhotoToLoad photoToLoad){
  135. this.photoToLoad=photoToLoad;
  136. }
  137.  
  138. @Override
  139. public void run() {
  140. if(imageViewReused(photoToLoad))
  141. return;
  142. Bitmap bmp=getBitmap(photoToLoad.url);
  143. memoryCache.put(photoToLoad.url, bmp);
  144. if(imageViewReused(photoToLoad))
  145. return;
  146. BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
  147. Activity a=(Activity)photoToLoad.imageView.getContext();
  148. a.runOnUiThread(bd);
  149. }
  150. }
  151.  
  152. boolean imageViewReused(PhotoToLoad photoToLoad){
  153. String tag=imageViews.get(photoToLoad.imageView);
  154. if(tag==null || !tag.equals(photoToLoad.url))
  155. return true;
  156. return false;
  157. }
  158.  
  159. //Used to display bitmap in the UI thread
  160. class BitmapDisplayer implements Runnable
  161. {
  162. Bitmap bitmap;
  163. PhotoToLoad photoToLoad;
  164. public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
  165. public void run()
  166. {
  167. if(imageViewReused(photoToLoad))
  168. return;
  169. if(bitmap!=null)
  170. photoToLoad.imageView.setImageBitmap(bitmap);
  171. else
  172. photoToLoad.imageView.setImageResource(stub_id);
  173. }
  174. }
  175.  
  176. public void clearCache() {
  177. memoryCache.clear();
  178. fileCache.clear();
  179. }
  180.  
  181. }
Add Comment
Please, Sign In to add comment