vitaly m
- Forum posts: 1
Nov 12, 2011, 5:20:44 PM via Website
Nov 12, 2011 5:20:44 PM via Website
Hello all!
I have a problem with display img + text on listview. I have a code that works ok on simulator but when I install my code on my samsung galaxy s I cant see the pic near the text.
I don't know what the problem because on my simulator its work fine.
Maybe because I don't have an sd card? how can I change my code that it will work ok on my device?
the code is:
on getView
imageLoader.displayImage(imgUrl, activity, ImageView);
I have a problem with display img + text on listview. I have a code that works ok on simulator but when I install my code on my samsung galaxy s I cant see the pic near the text.
I don't know what the problem because on my simulator its work fine.
Maybe because I don't have an sd card? how can I change my code that it will work ok on my device?
the code is:
on getView
imageLoader.displayImage(imgUrl, activity, ImageView);
1package rusNewsTest;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.net.URL;
10import java.util.HashMap;
11import java.util.Stack;
12import com.example.News.R;
13import android.app.Activity;
14import android.content.Context;
15import android.graphics.Bitmap;
16import android.graphics.BitmapFactory;
17import android.widget.ImageView;
18
19public class ImageThreadLoader {
20
21 //the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
22 /** The cache. */
23 private HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();
24
25 /** The cache dir. */
26 private File cacheDir;
27
28 /**
29 * Instantiates a new image loader.
30 *
31 * @param context the context
32 */
33 public ImageThreadLoader(Context context){
34 //Make the background thead low priority. This way it will not affect the UI performance
35 photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
36
37 //Find the dir to save cached images
38 if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
39 cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"cache_dir_img");
40 else
41 cacheDir=context.getCacheDir();
42 if(!cacheDir.exists())
43 cacheDir.mkdirs();
44 }
45 //This is used for a stub when the user can not see the actual image..
46 //this images will be seen
47 final int stub_id =R.drawable.stub;
48
49
50 /**
51 * Display image.
52 *
53 * @param url the url
54 * @param activity the activity
55 * @param imageView the image view
56 */
57 public void displayImage(String url, Activity activity, ImageView imageView)
58 {
59 if(cache.containsKey(url))
60 imageView.setImageBitmap(cache.get(url));
61 else
62 {
63 queuePhoto(url, activity, imageView);
64 imageView.setImageResource(stub_id);
65 }
66 }
67
68 /**
69 * Queue photo.
70 *
71 * @param url the url
72 * @param activity the activity
73 * @param imageView the image view
74 */
75 private void queuePhoto(String url, Activity activity, ImageView imageView)
76 {
77 //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them.
78 photosQueue.Clean(imageView);
79 PhotoToLoad p=new PhotoToLoad(url, imageView);
80 synchronized(photosQueue.photosToLoad){
81 photosQueue.photosToLoad.push(p);
82 photosQueue.photosToLoad.notifyAll();
83 }
84
85 //start thread if it's not started yet
86 if(photoLoaderThread.getState()==Thread.State.NEW)
87 photoLoaderThread.start();
88 }
89
90 /**
91 * Gets the bitmap.
92 *
93 * @param url the url
94 * @return the bitmap
95 */
96 private Bitmap getBitmap(String url)
97 {
98 //I identify images by hashcode. Not a perfect solution, good for the demo.
99 String filename=String.valueOf(url.hashCode());
100 File f=new File(cacheDir, filename);
101
102 //from SD cache
103 Bitmap b = decodeFile(f);
104 if(b!=null)
105 return b;
106
107 //from web
108 try {
109 Bitmap bitmap=null;
110 InputStream is=new URL(url).openStream();
111 OutputStream os = new FileOutputStream(f);
112 copyStream(is, os);
113 os.close();
114 bitmap = decodeFile(f);
115 return bitmap;
116 } catch (Exception ex){
117 ex.printStackTrace();
118 return null;
119 }
120 }
121
122 //decodes image and scales it to reduce memory consumption
123 /**
124 * Decode file.
125 *
126 * @param f the f
127 * @return the bitmap
128 */
129 private Bitmap decodeFile(File f){
130 try {
131 //decode image size
132 BitmapFactory.Options o = new BitmapFactory.Options();
133 o.inJustDecodeBounds = true;
134 BitmapFactory.decodeStream(new FileInputStream(f),null,o);
135
136 //Find the correct scale value. It should be the power of 2.
137 final int REQUIRED_SIZE=70;
138 int width_tmp=o.outWidth, height_tmp=o.outHeight;
139 int scale=1;
140 while(true){
141 if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
142 break;
143 width_tmp/=2;
144 height_tmp/=2;
145 scale++;
146 }
147
148 //decode with inSampleSize
149 BitmapFactory.Options o2 = new BitmapFactory.Options();
150 o2.inSampleSize=scale;
151 return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
152 } catch (FileNotFoundException e) {}
153 return null;
154 }
155
156 //Task for the queue
157 /**
158 * The Class PhotoToLoad.
159 */
160 private class PhotoToLoad
161 {
162
163 /** The url. */
164 public String url;
165
166 /** The image view. */
167 public ImageView imageView;
168
169 /**
170 * Instantiates a new photo to load.
171 *
172 * @param u the u
173 * @param i the i
174 */
175 public PhotoToLoad(String u, ImageView i){
176 url=u;
177 imageView=i;
178 }
179 }
180
181 /** The photos queue. */
182 PhotosQueue photosQueue=new PhotosQueue();
183
184 /**
185 * Stop thread.
186 */
187 public void stopThread()
188 {
189 photoLoaderThread.interrupt();
190 }
191
192 //stores list of photos to download
193 /**
194 * The Class PhotosQueue.
195 */
196 class PhotosQueue
197 {
198
199 /** The photos to load. */
200 private Stack<PhotoToLoad> photosToLoad=new Stack<PhotoToLoad>();
201
202 //removes all instances of this ImageView
203 /**
204 * Clean.
205 *
206 * @param image the image
207 */
208 public void Clean(ImageView image)
209 {
210 for(int j=0 ;j<photosToLoad.size();){
211 if(photosToLoad.get(j).imageView==image)
212 photosToLoad.remove(j);
213 else
214 ++j;
215 }
216 }
217 }
218
219 /**
220 * The Class PhotosLoader.
221 */
222 class PhotosLoader extends Thread {
223
224 /* (non-Javadoc)
225 * @see java.lang.Thread#run()
226 */
227 public void run() {
228 try {
229 while(true)
230 {
231 //thread waits until there are any images to load in the queue
232 if(photosQueue.photosToLoad.size()==0)
233 synchronized(photosQueue.photosToLoad){
234 photosQueue.photosToLoad.wait();
235 }
236 if(photosQueue.photosToLoad.size()!=0)
237 {
238 PhotoToLoad photoToLoad;
239 synchronized(photosQueue.photosToLoad){
240 photoToLoad=photosQueue.photosToLoad.pop();
241 }
242 Bitmap bmp=getBitmap(photoToLoad.url);
243 cache.put(photoToLoad.url, bmp);
244 if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){
245 BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
246 Activity a=(Activity)photoToLoad.imageView.getContext();
247 a.runOnUiThread(bd);
248 }
249 }
250 if(Thread.interrupted())
251 break;
252 }
253 } catch (InterruptedException e) {
254 //allow thread to exit
255 }
256 }
257 }
258
259 /** The photo loader thread. */
260 PhotosLoader photoLoaderThread=new PhotosLoader();
261
262 //Used to display bitmap in the UI thread
263 /**
264 * The Class BitmapDisplayer.
265 */
266 class BitmapDisplayer implements Runnable
267 {
268
269 /** The bitmap. */
270 Bitmap bitmap;
271
272 /** The image view. */
273 ImageView imageView;
274
275 /**
276 * Instantiates a new bitmap displayer.
277 *
278 * @param b the b
279 * @param i the i
280 */
281 public BitmapDisplayer(Bitmap b, ImageView i){bitmap=b;imageView=i;}
282
283 /* (non-Javadoc)
284 * @see java.lang.Runnable#run()
285 */
286 public void run()
287 {
288 if(bitmap!=null)
289 imageView.setImageBitmap(bitmap);
290 else
291 imageView.setImageResource(stub_id);
292 }
293 }
294
295 /**
296 * Clear cache.
297 */
298 public void clearCache() {
299 //clear memory cache
300 cache.clear();
301
302 //clear SD cache
303 File[] files=cacheDir.listFiles();
304 for(File f:files)
305 f.delete();
306 }
307
308 public static void copyStream(InputStream is, OutputStream os) {
309 final int buffer_size=1024;
310 try
311 {
312 byte[] bytes=new byte[buffer_size];
313 for(;;)
314 {
315 int count=is.read(bytes, 0, buffer_size);
316 if(count==-1)
317 break;
318 os.write(bytes, 0, count);
319 }
320 }
321 catch(Exception ex){}
322 }
323
324
325}
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.net.URL;
10import java.util.HashMap;
11import java.util.Stack;
12import com.example.News.R;
13import android.app.Activity;
14import android.content.Context;
15import android.graphics.Bitmap;
16import android.graphics.BitmapFactory;
17import android.widget.ImageView;
18
19public class ImageThreadLoader {
20
21 //the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
22 /** The cache. */
23 private HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();
24
25 /** The cache dir. */
26 private File cacheDir;
27
28 /**
29 * Instantiates a new image loader.
30 *
31 * @param context the context
32 */
33 public ImageThreadLoader(Context context){
34 //Make the background thead low priority. This way it will not affect the UI performance
35 photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
36
37 //Find the dir to save cached images
38 if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
39 cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"cache_dir_img");
40 else
41 cacheDir=context.getCacheDir();
42 if(!cacheDir.exists())
43 cacheDir.mkdirs();
44 }
45 //This is used for a stub when the user can not see the actual image..
46 //this images will be seen
47 final int stub_id =R.drawable.stub;
48
49
50 /**
51 * Display image.
52 *
53 * @param url the url
54 * @param activity the activity
55 * @param imageView the image view
56 */
57 public void displayImage(String url, Activity activity, ImageView imageView)
58 {
59 if(cache.containsKey(url))
60 imageView.setImageBitmap(cache.get(url));
61 else
62 {
63 queuePhoto(url, activity, imageView);
64 imageView.setImageResource(stub_id);
65 }
66 }
67
68 /**
69 * Queue photo.
70 *
71 * @param url the url
72 * @param activity the activity
73 * @param imageView the image view
74 */
75 private void queuePhoto(String url, Activity activity, ImageView imageView)
76 {
77 //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them.
78 photosQueue.Clean(imageView);
79 PhotoToLoad p=new PhotoToLoad(url, imageView);
80 synchronized(photosQueue.photosToLoad){
81 photosQueue.photosToLoad.push(p);
82 photosQueue.photosToLoad.notifyAll();
83 }
84
85 //start thread if it's not started yet
86 if(photoLoaderThread.getState()==Thread.State.NEW)
87 photoLoaderThread.start();
88 }
89
90 /**
91 * Gets the bitmap.
92 *
93 * @param url the url
94 * @return the bitmap
95 */
96 private Bitmap getBitmap(String url)
97 {
98 //I identify images by hashcode. Not a perfect solution, good for the demo.
99 String filename=String.valueOf(url.hashCode());
100 File f=new File(cacheDir, filename);
101
102 //from SD cache
103 Bitmap b = decodeFile(f);
104 if(b!=null)
105 return b;
106
107 //from web
108 try {
109 Bitmap bitmap=null;
110 InputStream is=new URL(url).openStream();
111 OutputStream os = new FileOutputStream(f);
112 copyStream(is, os);
113 os.close();
114 bitmap = decodeFile(f);
115 return bitmap;
116 } catch (Exception ex){
117 ex.printStackTrace();
118 return null;
119 }
120 }
121
122 //decodes image and scales it to reduce memory consumption
123 /**
124 * Decode file.
125 *
126 * @param f the f
127 * @return the bitmap
128 */
129 private Bitmap decodeFile(File f){
130 try {
131 //decode image size
132 BitmapFactory.Options o = new BitmapFactory.Options();
133 o.inJustDecodeBounds = true;
134 BitmapFactory.decodeStream(new FileInputStream(f),null,o);
135
136 //Find the correct scale value. It should be the power of 2.
137 final int REQUIRED_SIZE=70;
138 int width_tmp=o.outWidth, height_tmp=o.outHeight;
139 int scale=1;
140 while(true){
141 if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
142 break;
143 width_tmp/=2;
144 height_tmp/=2;
145 scale++;
146 }
147
148 //decode with inSampleSize
149 BitmapFactory.Options o2 = new BitmapFactory.Options();
150 o2.inSampleSize=scale;
151 return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
152 } catch (FileNotFoundException e) {}
153 return null;
154 }
155
156 //Task for the queue
157 /**
158 * The Class PhotoToLoad.
159 */
160 private class PhotoToLoad
161 {
162
163 /** The url. */
164 public String url;
165
166 /** The image view. */
167 public ImageView imageView;
168
169 /**
170 * Instantiates a new photo to load.
171 *
172 * @param u the u
173 * @param i the i
174 */
175 public PhotoToLoad(String u, ImageView i){
176 url=u;
177 imageView=i;
178 }
179 }
180
181 /** The photos queue. */
182 PhotosQueue photosQueue=new PhotosQueue();
183
184 /**
185 * Stop thread.
186 */
187 public void stopThread()
188 {
189 photoLoaderThread.interrupt();
190 }
191
192 //stores list of photos to download
193 /**
194 * The Class PhotosQueue.
195 */
196 class PhotosQueue
197 {
198
199 /** The photos to load. */
200 private Stack<PhotoToLoad> photosToLoad=new Stack<PhotoToLoad>();
201
202 //removes all instances of this ImageView
203 /**
204 * Clean.
205 *
206 * @param image the image
207 */
208 public void Clean(ImageView image)
209 {
210 for(int j=0 ;j<photosToLoad.size();){
211 if(photosToLoad.get(j).imageView==image)
212 photosToLoad.remove(j);
213 else
214 ++j;
215 }
216 }
217 }
218
219 /**
220 * The Class PhotosLoader.
221 */
222 class PhotosLoader extends Thread {
223
224 /* (non-Javadoc)
225 * @see java.lang.Thread#run()
226 */
227 public void run() {
228 try {
229 while(true)
230 {
231 //thread waits until there are any images to load in the queue
232 if(photosQueue.photosToLoad.size()==0)
233 synchronized(photosQueue.photosToLoad){
234 photosQueue.photosToLoad.wait();
235 }
236 if(photosQueue.photosToLoad.size()!=0)
237 {
238 PhotoToLoad photoToLoad;
239 synchronized(photosQueue.photosToLoad){
240 photoToLoad=photosQueue.photosToLoad.pop();
241 }
242 Bitmap bmp=getBitmap(photoToLoad.url);
243 cache.put(photoToLoad.url, bmp);
244 if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){
245 BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
246 Activity a=(Activity)photoToLoad.imageView.getContext();
247 a.runOnUiThread(bd);
248 }
249 }
250 if(Thread.interrupted())
251 break;
252 }
253 } catch (InterruptedException e) {
254 //allow thread to exit
255 }
256 }
257 }
258
259 /** The photo loader thread. */
260 PhotosLoader photoLoaderThread=new PhotosLoader();
261
262 //Used to display bitmap in the UI thread
263 /**
264 * The Class BitmapDisplayer.
265 */
266 class BitmapDisplayer implements Runnable
267 {
268
269 /** The bitmap. */
270 Bitmap bitmap;
271
272 /** The image view. */
273 ImageView imageView;
274
275 /**
276 * Instantiates a new bitmap displayer.
277 *
278 * @param b the b
279 * @param i the i
280 */
281 public BitmapDisplayer(Bitmap b, ImageView i){bitmap=b;imageView=i;}
282
283 /* (non-Javadoc)
284 * @see java.lang.Runnable#run()
285 */
286 public void run()
287 {
288 if(bitmap!=null)
289 imageView.setImageBitmap(bitmap);
290 else
291 imageView.setImageResource(stub_id);
292 }
293 }
294
295 /**
296 * Clear cache.
297 */
298 public void clearCache() {
299 //clear memory cache
300 cache.clear();
301
302 //clear SD cache
303 File[] files=cacheDir.listFiles();
304 for(File f:files)
305 f.delete();
306 }
307
308 public static void copyStream(InputStream is, OutputStream os) {
309 final int buffer_size=1024;
310 try
311 {
312 byte[] bytes=new byte[buffer_size];
313 for(;;)
314 {
315 int count=is.read(bytes, 0, buffer_size);
316 if(count==-1)
317 break;
318 os.write(bytes, 0, count);
319 }
320 }
321 catch(Exception ex){}
322 }
323
324
325}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.