ImageView Refresh image from internet periodicly

All your problems with Audio, Video and Images.

ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Fri Aug 27, 2010 12:34 pm

Hi!,
I'm new to android and you could say new to java also. I need help with following issue. I want to make an android application which sets imageView image from internet and I need it to be refreshed every 2 seconds.

I did the downloading and setting the image but I'm having trouble with refreshing that image!

Any help is apreciated,
Antonio
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Top

Re: ImageView Refresh image from internet periodicly

Postby blundell » Fri Aug 27, 2010 2:44 pm

You would need to spawn a thread with a timer, then every two seconds download the new image. Then in your UI thread have a handler that updates the image view
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Sat Aug 28, 2010 1:33 pm

blundell wrote:You would need to spawn a thread with a timer, then every two seconds download the new image. Then in your UI thread have a handler that updates the image view



Could you please be so kind and write me a sample of that code. I'm a beginner and do not know that much to do that in a short time and i need it fast.
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Re: ImageView Refresh image from internet periodicly

Postby jonbonazza » Sat Aug 28, 2010 10:41 pm

I haven't tested this, as I don't have time, but something like this should work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ExampleActivity extends Activity
  2. {
  3.         ImageView image;
  4.         Timer timer;
  5.         @Override
  6.         public void onCreate(Bundle savedInstanceState)
  7.         {
  8.                 super.onCreate(savedInstanceState);
  9.                 image = (ImageView)findViewById(R.id.splash_logo);
  10.                 TimerThread timerThread = new TimerThread();
  11.                 timerThread.start();
  12.         }
  13.        
  14.         private class TimerThread extends Thread
  15.         {
  16.                 public void run()
  17.                 {
  18.                         timer = new Timer();
  19.                         timer.schedule(new DownloadTimerTask(), 2000);
  20.                 }
  21.         }
  22.         private class DownloadTimerTask extends TimerTask
  23.         {
  24.                 public void run()
  25.                 {
  26.                         downloadImage();
  27.                         handler.sendEmptyMessage(0);
  28.                 }
  29.         }Handlerdler handler = new Handler()
  30.         {
  31.                 @Override
  32.                 public void handleMessage(Message msg)
  33.                 {
  34.                         image.setImageBitmap
  35.                 }
  36.         }
  37.        
  38.         private void downloadImage()
  39.         {      
  40.                 String filePath = "/sdcard/download";
  41.                 File file = new File(filePath);
  42.                 InputStream in = null;
  43.                 String urlString = yourURL;
  44.                 BufferedInputStream bis = null;
  45.                 try
  46.                 {
  47.                         URL url = new URL(urlString);
  48.                         URLConnection ucon = url.openConnection();
  49.                         in = ucon.getInputStream();
  50.                         bis = new BufferedInputStream(in);
  51.                        
  52.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  53.                        
  54.                         int current = 0;
  55.                        
  56.                         while((current = bis.read()) != -1)
  57.                         {
  58.                                 baf.append((byte)current);
  59.                         }
  60.                        
  61.                         FileOutputStream fos = new FileOutputStream(file);
  62.                         fos.write(baf.toByteArray());
  63.                         fos.close();
  64.                         bis.close();
  65.                         in.close();
  66.                 }
  67.                 catch (MalformedURLException e)
  68.                 {
  69.                         e.printStackTrace();
  70.                 }
  71.                 catch(IOException ex)
  72.                 {
  73.                         ex.printStackTrace();
  74.                 }
  75.         }
  76. }
  77.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: ImageView Refresh image from internet periodicly

Postby blundell » Sat Aug 28, 2010 10:56 pm

There you go beat me to it :-) just have to fix some spellin errors 'Handlerdler' but conveys the point :-D
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: ImageView Refresh image from internet periodicly

Postby jonbonazza » Sat Aug 28, 2010 11:05 pm

Yea, and I forgot to finsih the handler. XD Here is the fixed code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ExampleActivity extends Activity
  2. {
  3.         ImageView image;
  4.         Timer timer;
  5.         @Override
  6.         public void onCreate(Bundle savedInstanceState)
  7.         {
  8.                 super.onCreate(savedInstanceState);
  9.                 image = (ImageView)findViewById(R.id.splash_logo);
  10.                 TimerThread timerThread = new TimerThread();
  11.                 timerThread.start();
  12.         }
  13.        
  14.         private class TimerThread extends Thread
  15.         {
  16.                 public void run()
  17.                 {
  18.                         timer = new Timer();
  19.                         timer.schedule(new DownloadTimerTask(), 2000);
  20.                 }
  21.         }
  22.         private class DownloadTimerTask extends TimerTask
  23.         {
  24.                 public void run()
  25.                 {
  26.                         downloadImage();
  27.                         handler.sendEmptyMessage(0);
  28.                 }
  29.         }
  30.        
  31.         Handler handler = new Handler()
  32.         {
  33.                 final String path = "/sdcard/.MitsuMedia/rotm/" + imageName;
  34.                 Bitmap bitmap;
  35.                 File file = new File(path);
  36.                 @Override
  37.                 public void handleMessage(Message msg)
  38.                 {
  39.                         bitmap = BitmapFactory.decodeFile(path);
  40.                         image.setImageBitmap(bitmap);
  41.                 }
  42.         };
  43.        
  44.         private void downloadImage()
  45.         {      
  46.                 String filePath = "/sdcard/download";
  47.                 File file = new File(filePath);
  48.                 InputStream in = null;
  49.                 String urlString = yourURL;
  50.                 BufferedInputStream bis = null;
  51.                 try
  52.                 {
  53.                         URL url = new URL(urlString);
  54.                         URLConnection ucon = url.openConnection();
  55.                         in = ucon.getInputStream();
  56.                         bis = new BufferedInputStream(in);
  57.                        
  58.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  59.                        
  60.                         int current = 0;
  61.                        
  62.                         while((current = bis.read()) != -1)
  63.                         {
  64.                                 baf.append((byte)current);
  65.                         }
  66.                        
  67.                         FileOutputStream fos = new FileOutputStream(file);
  68.                         fos.write(baf.toByteArray());
  69.                         fos.close();
  70.                         bis.close();
  71.                         in.close();
  72.                 }
  73.                 catch (MalformedURLException e)
  74.                 {
  75.                         e.printStackTrace();
  76.                 }
  77.                 catch(IOException ex)
  78.                 {
  79.                         ex.printStackTrace();
  80.                 }
  81.         }
  82. }
  83.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Sun Aug 29, 2010 9:24 pm

Thank you for your help! I wasn't near computer so i couldn't answer earlier. I will try the code and then let you know how i went.

thanks once again
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Mon Aug 30, 2010 1:32 pm

Maybe now I'm asking too much but since you have already answered this, i need some more help. :)
I have two imagebuttons, idea is to view one image with one button and another wit the other one. I did this but without the timer addon that you gave me, thanx again fo that.

If you could help me out please.


And another thing, that is offtopic but...
I would really like to learn android developmnet but i'm missing some essential things, so could you please give me some references from where to start from scratch. OOP, Java, Android,... Any books, tutorials, anything that can help. I don't know much about any of that and i would really like to know as you guys do :)

Best regards,
Antonio
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Re: ImageView Refresh image from internet periodicly

Postby jonbonazza » Mon Aug 30, 2010 7:00 pm

I am not exactly sure what you are asking for help with on the first part.

As for references, What is your programming background? Do you know java? Do you know any other languages? Are you familiar with OOP in general?
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Mon Aug 30, 2010 8:06 pm

jonbonazza wrote:I am not exactly sure what you are asking for help with on the first part.

As for references, What is your programming background? Do you know java? Do you know any other languages? Are you familiar with OOP in general?


1. I need to have two buttons each one refreshing different image from internet. It is a class project which i have to do.

2. I'm familiar with oop but i need to do some refreshing because i've spent the last two years programming in VB6( developing some old app.) I would like to go from scratch, repeat oop (some book, reference would be nice). Java, also some basic things (mostly some "hello world" apps.).
Reserching for Android was really cool and interesting so I would like to learn Android programming eventually, I know that i need to know Java very good to deveop for Android so any information and tip are welcome.
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Tue Aug 31, 2010 10:37 am

jonbonazza wrote:Yea, and I forgot to finsih the handler. XD Here is the fixed code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ExampleActivity extends Activity
  2. {
  3.         ImageView image;
  4.         Timer timer;
  5.         @Override
  6.         public void onCreate(Bundle savedInstanceState)
  7.         {
  8.                 super.onCreate(savedInstanceState);
  9.                 image = (ImageView)findViewById(R.id.splash_logo);
  10.                 TimerThread timerThread = new TimerThread();
  11.                 timerThread.start();
  12.         }
  13.        
  14.         private class TimerThread extends Thread
  15.         {
  16.                 public void run()
  17.                 {
  18.                         timer = new Timer();
  19.                         timer.schedule(new DownloadTimerTask(), 2000);
  20.                 }
  21.         }
  22.         private class DownloadTimerTask extends TimerTask
  23.         {
  24.                 public void run()
  25.                 {
  26.                         downloadImage();
  27.                         handler.sendEmptyMessage(0);
  28.                 }
  29.         }
  30.        
  31.         Handler handler = new Handler()
  32.         {
  33.                 final String path = "/sdcard/.MitsuMedia/rotm/" + imageName;
  34.                 Bitmap bitmap;
  35.                 File file = new File(path);
  36.                 @Override
  37.                 public void handleMessage(Message msg)
  38.                 {
  39.                         bitmap = BitmapFactory.decodeFile(path);
  40.                         image.setImageBitmap(bitmap);
  41.                 }
  42.         };
  43.        
  44.         private void downloadImage()
  45.         {      
  46.                 String filePath = "/sdcard/download";
  47.                 File file = new File(filePath);
  48.                 InputStream in = null;
  49.                 String urlString = yourURL;
  50.                 BufferedInputStream bis = null;
  51.                 try
  52.                 {
  53.                         URL url = new URL(urlString);
  54.                         URLConnection ucon = url.openConnection();
  55.                         in = ucon.getInputStream();
  56.                         bis = new BufferedInputStream(in);
  57.                        
  58.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  59.                        
  60.                         int current = 0;
  61.                        
  62.                         while((current = bis.read()) != -1)
  63.                         {
  64.                                 baf.append((byte)current);
  65.                         }
  66.                        
  67.                         FileOutputStream fos = new FileOutputStream(file);
  68.                         fos.write(baf.toByteArray());
  69.                         fos.close();
  70.                         bis.close();
  71.                         in.close();
  72.                 }
  73.                 catch (MalformedURLException e)
  74.                 {
  75.                         e.printStackTrace();
  76.                 }
  77.                 catch(IOException ex)
  78.                 {
  79.                         ex.printStackTrace();
  80.                 }
  81.         }
  82. }
  83.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


I'm sorry but his code is not working for me. :( this is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package droid.test.vn.droid;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.util.Timer;
  12. import java.util.TimerTask;
  13.  
  14. import org.apache.http.util.ByteArrayBuffer;
  15.  
  16. import droid.test.vn.R;
  17.  
  18. import android.app.Activity;
  19. import android.graphics.Bitmap;
  20. import android.graphics.BitmapFactory;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.view.View;
  25. import android.widget.Button;
  26. import android.widget.ImageButton;
  27. import android.widget.ImageView;
  28.  
  29. public class test extends Activity{
  30.     /** Called when the activity is first created. */
  31.  
  32.         ImageView image;
  33.         Timer timer;
  34.         @Override
  35.         public void onCreate(Bundle savedInstanceState)
  36.         {
  37.                 super.onCreate(savedInstanceState);
  38.                 image=(ImageView)findViewById(R.id.imgView);
  39.                 TimerThread timerThread = new TimerThread();
  40.                 timerThread.start();
  41.         }
  42.        
  43.         private class TimerThread extends Thread
  44.         {
  45.                 public void run()
  46.                 {
  47.                         timer = new Timer();
  48.                         timer.schedule(new DownloadTimerTask(),2000);
  49.                 }
  50.         }
  51.         private class DownloadTimerTask extends TimerTask
  52.         {
  53.                 public void run()
  54.                 {
  55.                         downloadImage();
  56.                         handler.sendEmptyMessage(0);
  57.                 }
  58.         }
  59.        
  60.         Handler handler = new Handler()
  61.         {
  62.                 final String path ="/sdcard/.MitsuMedia/rotm/temp.jpg";
  63.                 Bitmap bitmap;
  64.                 File file = new File(path);
  65.                 @Override
  66.                 public void handleMessage(Message msg)
  67.                 {
  68.                         bitmap = BitmapFactory.decodeFile(path);
  69.                         image.setImageBitmap(bitmap);
  70.                 }
  71.         };
  72.        
  73.         private void downloadImage()
  74.         {
  75.                 String filePath = "/sdcard/download";
  76.                 File file = new File(filePath);
  77.                 InputStream in = null;
  78.                 String urlString = "http://www.croatianstar.com/cam1/current.jpg";
  79.                 BufferedInputStream bis = null;
  80.                 try
  81.                 {
  82.                         URL url = new URL(urlString);
  83.                         URLConnection ucon = url.openConnection();
  84.                         in = ucon.getInputStream();
  85.                         bis = new BufferedInputStream(in);
  86.                        
  87.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  88.                         int  current = 0;
  89.                        
  90.                         while((current = bis.read())!=-1)
  91.                         {
  92.                                 baf.append((byte)current);
  93.                         }
  94.                        
  95.                         FileOutputStream fos = new FileOutputStream(file);
  96.                         fos.write(baf.toByteArray());
  97.                         fos.close();
  98.                         bis.close();
  99.                         in.close();
  100.                        
  101.                 }
  102.                 catch(MalformedURLException e)
  103.                 {
  104.                         e.printStackTrace();
  105.                 }
  106.                 catch (IOException ex)
  107.                 {
  108.                         ex.printStackTrace();
  109.                 }
  110.         }
  111.        
  112. }
  113.  
  114.  
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


I think that could be something with the saving file: filePath. Because you have a line that says: final String path = "/sdcard/.MitsuMedia/rotm/" + imageName;

and eclipse reports an error because it cannot find: imageName
I have replaced the imageName with "temp.jpg", but i still can't get it to work.
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Re: ImageView Refresh image from internet periodicly

Postby blundell » Tue Aug 31, 2010 12:40 pm

Your downloading the image to here:
String filePath = "/sdcard/download";

but then attempting to load it from here:
final String path ="/sdcard/.MitsuMedia/rotm/temp.jpg";

so yes it can't find the file
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Tue Aug 31, 2010 1:53 pm

blundell wrote:Your downloading the image to here:
String filePath = "/sdcard/download";

but then attempting to load it from here:
final String path ="/sdcard/.MitsuMedia/rotm/temp.jpg";

so yes it can't find the file


I've been trying to get it work but no sucess. When I debug the app it says "Source not found"
I have removed all the data and just wanted to set some image from my resources. "R.drawable.Cam" and still no luck!
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Re: ImageView Refresh image from internet periodicly

Postby jonbonazza » Tue Aug 31, 2010 4:49 pm

Woops... I accidentally forgot to change that line. lol I actually copied and pasted most of that from an earlier project I did.
Try this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11.  
  12. import org.apache.http.util.ByteArrayBuffer;
  13.  
  14. import droid.test.vn.R;
  15.  
  16. import android.app.Activity;
  17. import android.graphics.Bitmap;
  18. import android.graphics.BitmapFactory;
  19. import android.os.Bundle;
  20. import android.os.Handler;
  21. import android.os.Message;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.ImageButton;
  25. import android.widget.ImageView;
  26.  
  27. public class test extends Activity{
  28.          public static final String imageName = "my_image.jpg";   //the name of your image file goes here
  29.         ImageView image;
  30.         Timer timer;
  31.         @Override
  32.         public void onCreate(Bundle savedInstanceState)
  33.         {
  34.                 super.onCreate(savedInstanceState);
  35.                 image=(ImageView)findViewById(R.id.imgView);
  36.                 TimerThread timerThread = new TimerThread();
  37.                 timerThread.start();
  38.         }
  39.        
  40.         private class TimerThread extends Thread
  41.         {
  42.                 public void run()
  43.                 {
  44.                         timer = new Timer();
  45.                         timer.schedule(new DownloadTimerTask(),2000);
  46.                 }
  47.         }
  48.         private class DownloadTimerTask extends TimerTask
  49.         {
  50.                 public void run()
  51.                 {
  52.                         downloadImage();
  53.                         handler.sendEmptyMessage(0);
  54.                 }
  55.         }
  56.        
  57.         Handler handler = new Handler()
  58.         {
  59.                 final String path ="/sdcard/download" + imageName;
  60.                 Bitmap bitmap;
  61.                 File file = new File(path);
  62.                 @Override
  63.                 public void handleMessage(Message msg)
  64.                 {
  65.                         bitmap = BitmapFactory.decodeFile(path);
  66.                         image.setImageBitmap(bitmap);
  67.                 }
  68.         };
  69.        
  70.         private void downloadImage()
  71.         {
  72.                 String filePath = "/sdcard/download";
  73.                 File file = new File(filePath);
  74.                 InputStream in = null;
  75.                 String urlString = "http://www.croatianstar.com/cam1/current.jpg";
  76.                 BufferedInputStream bis = null;
  77.                 try
  78.                 {
  79.                         URL url = new URL(urlString);
  80.                         URLConnection ucon = url.openConnection();
  81.                         in = ucon.getInputStream();
  82.                         bis = new BufferedInputStream(in);
  83.                        
  84.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  85.                         int  current = 0;
  86.                        
  87.                         while((current = bis.read())!=-1)
  88.                         {
  89.                                 baf.append((byte)current);
  90.                         }
  91.                        
  92.                         FileOutputStream fos = new FileOutputStream(file);
  93.                         fos.write(baf.toByteArray());
  94.                         fos.close();
  95.                         bis.close();
  96.                         in.close();
  97.                        
  98.                 }
  99.                 catch(MalformedURLException e)
  100.                 {
  101.                         e.printStackTrace();
  102.                 }
  103.                 catch (IOException ex)
  104.                 {
  105.                         ex.printStackTrace();
  106.                 }
  107.         }
  108.        
  109. }
  110.  
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


imageName should be a class-wide constant that is a string object containing the name of your image file. For example, you might define it like so:

public static final String imageName = "my_image.jpg";

I didn't include this in the code, so
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: ImageView Refresh image from internet periodicly

Postby dujmovic.antonio » Tue Aug 31, 2010 5:03 pm

jonbonazza wrote:Woops... I accidentally forgot to change that line. lol I actually copied and pasted most of that from an earlier project I did.
Try this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11.  
  12. import org.apache.http.util.ByteArrayBuffer;
  13.  
  14. import droid.test.vn.R;
  15.  
  16. import android.app.Activity;
  17. import android.graphics.Bitmap;
  18. import android.graphics.BitmapFactory;
  19. import android.os.Bundle;
  20. import android.os.Handler;
  21. import android.os.Message;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.ImageButton;
  25. import android.widget.ImageView;
  26.  
  27. public class test extends Activity{
  28.          public static final String imageName = "my_image.jpg";   //the name of your image file goes here
  29.         ImageView image;
  30.         Timer timer;
  31.         @Override
  32.         public void onCreate(Bundle savedInstanceState)
  33.         {
  34.                 super.onCreate(savedInstanceState);
  35.                 image=(ImageView)findViewById(R.id.imgView);
  36.                 TimerThread timerThread = new TimerThread();
  37.                 timerThread.start();
  38.         }
  39.        
  40.         private class TimerThread extends Thread
  41.         {
  42.                 public void run()
  43.                 {
  44.                         timer = new Timer();
  45.                         timer.schedule(new DownloadTimerTask(),2000);
  46.                 }
  47.         }
  48.         private class DownloadTimerTask extends TimerTask
  49.         {
  50.                 public void run()
  51.                 {
  52.                         downloadImage();
  53.                         handler.sendEmptyMessage(0);
  54.                 }
  55.         }
  56.        
  57.         Handler handler = new Handler()
  58.         {
  59.                 final String path ="/sdcard/download" + imageName;
  60.                 Bitmap bitmap;
  61.                 File file = new File(path);
  62.                 @Override
  63.                 public void handleMessage(Message msg)
  64.                 {
  65.                         bitmap = BitmapFactory.decodeFile(path);
  66.                         image.setImageBitmap(bitmap);
  67.                 }
  68.         };
  69.        
  70.         private void downloadImage()
  71.         {
  72.                 String filePath = "/sdcard/download";
  73.                 File file = new File(filePath);
  74.                 InputStream in = null;
  75.                 String urlString = "http://www.croatianstar.com/cam1/current.jpg";
  76.                 BufferedInputStream bis = null;
  77.                 try
  78.                 {
  79.                         URL url = new URL(urlString);
  80.                         URLConnection ucon = url.openConnection();
  81.                         in = ucon.getInputStream();
  82.                         bis = new BufferedInputStream(in);
  83.                        
  84.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  85.                         int  current = 0;
  86.                        
  87.                         while((current = bis.read())!=-1)
  88.                         {
  89.                                 baf.append((byte)current);
  90.                         }
  91.                        
  92.                         FileOutputStream fos = new FileOutputStream(file);
  93.                         fos.write(baf.toByteArray());
  94.                         fos.close();
  95.                         bis.close();
  96.                         in.close();
  97.                        
  98.                 }
  99.                 catch(MalformedURLException e)
  100.                 {
  101.                         e.printStackTrace();
  102.                 }
  103.                 catch (IOException ex)
  104.                 {
  105.                         ex.printStackTrace();
  106.                 }
  107.         }
  108.        
  109. }
  110.  
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


imageName should be a class-wide constant that is a string object containing the name of your image file. For example, you might define it like so:

public static final String imageName = "my_image.jpg";

I didn't include this in the code, so



Did you try this code?
Because I can't get it to work! I copied your code as is. Maybe is something wrong with my installation?
Do I have to have the image on my sdcard for this to work? sorry again if ask stupid questions. As I mentioned before my app nedds to have two buttons each refreshing a different image.
dujmovic.antonio
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Aug 27, 2010 12:27 pm

Top
Next

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests