Problem loading imageview

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem loading imageview

Postby androidrocks » Wed Mar 25, 2009 4:12 pm

Hi All,
I want to load an image from url, show them for fix interval and then show the next image again from another url. I tried doing this but the imageview does not show anything.

What i did is created a timer and in the timer i call a timertask which pulls the image from a url and places it in the imageview. The string which has url is changed dynamically so every time a new url is generated and the image is pulled from that url in the timertask. But the imageview doesnot show anything. It remains blank. I checked the url string and the string is correct.

This only happens when i loop the images. If i call a single image using one imageurl and if i dont loop it using timer or anything else it works fine. So the method that pulls the image is also correct.

Guys i am a complete newbie to andriod so please help if any ideas.
androidrocks
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 24, 2009 8:47 am

Top

Postby WarrenFaith » Wed Mar 25, 2009 4:57 pm

Ok, so show us your code and we can find the mistake. Anything else is just a look in the crystal ball...
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby WarrenFaith » Thu Mar 26, 2009 8:45 am

@androidrocks: please don't PM me with the same text you posted here.
Just give us more information and do it here in the forum and not over pm. thanks
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

this is the complete code that i have used

Postby androidrocks » Thu Mar 26, 2009 9:00 am

public class getimagefromweb extends Activity {

public static int i=1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

class MyAnimationRoutine extends TimerTask //timertask class which is called by timer below
{
public void run()
{
String str1="http://www.myurl.com/image"+i+".jpg";
ImageView imgView = (ImageView) findViewById(R.id.image1);
Drawable image = ImageOperations(getimagefromweb.this, str1,"image.jpg");
imgView.setImageDrawable(image);
i++;
}
}
MyAnimationRoutine mar = new MyAnimationRoutine();
Timer timer = new Timer(); //timer created
timer.schedule(mar,5000 ,50000);

}//on create close



private Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
androidrocks
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 24, 2009 8:47 am

Postby WarrenFaith » Thu Mar 26, 2009 9:12 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class getimagefromweb extends Activity {
  2.  
  3.    
  4.  
  5.     public static int i=1;
  6.  
  7.    
  8.  
  9.     class MyAnimationRoutine extends TimerTask
  10.  
  11.     {
  12.  
  13.         public void run()
  14.  
  15.         {
  16.  
  17.             String str1="http://www.myurl.com/image"+i+".jpg";
  18.  
  19.             ImageView imgView = (ImageView) findViewById(R.id.image1);
  20.  
  21.             Drawable image = ImageOperations(getimagefromweb.this, str1,"image.jpg");
  22.  
  23.             imgView.setImageDrawable(image);
  24.  
  25.             i++;
  26.  
  27.         }
  28.  
  29.     }
  30.  
  31.     MyAnimationRoutine mar = new MyAnimationRoutine();
  32.  
  33.     Timer timer = new Timer();
  34.  
  35.     timer.schedule(mar,5000 ,50000);
  36.  
  37.    
  38.  
  39.     private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
  40.  
  41.         try {
  42.  
  43.             InputStream is = (InputStream) this.fetch(url);
  44.  
  45.             Drawable d = Drawable.createFromStream(is, "src");
  46.  
  47.             return d;
  48.  
  49.         } catch (MalformedURLException e) {
  50.  
  51.             e.printStackTrace();
  52.  
  53.             return null;
  54.  
  55.         } catch (IOException e) {
  56.  
  57.             e.printStackTrace();
  58.  
  59.             return null;
  60.  
  61.         }
  62.  
  63.     }
  64.  
  65.    
  66.  
  67.     public Object fetch(String address) throws MalformedURLException, IOException {
  68.  
  69.         URL url = new URL(address);
  70.  
  71.         Object content = url.getContent();
  72.  
  73.         return content;
  74.  
  75.     }
  76.  
  77. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

Ok first: This class is never going to compile!
The line with "timer.schedule(mar,5000 ,50000);" is directly in the class and in no method. compile error number one!
Second: Some coding style would be nice... at least: Every class name starts with an upper case.
Third: Have you used the debugger to check which methods are called?
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

timer is called in oncreate method

Postby androidrocks » Thu Mar 26, 2009 10:29 am

I updated the code I am sorry actually i used the oncreate method to call the timer. I have edited the above posted code.

After debugging i check the class timertask is called and the method imageoperations is called which returns the drawable image and i placed it on the imagview but still the imageview remains blank. I have done debugging and checked that the imageoperations() is called.

The timertask class is also called at regular intervals but the imageview remains blank.

But still no result. Can u recheck and tell me wat is wrong with it.

I have edited the above posted code.
androidrocks
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 24, 2009 8:47 am

Top

Got this work

Postby androidrocks » Fri Mar 27, 2009 8:47 am

Thank you for your answers. I could make it work by replacing drawable with bitmap factory.

I guess there is some problem with the drawable class. I also replaced the timer with handler.

Handler is anyday better than the timer so for any scheduled task i would suggest everybody to use handler instead of timer.

So 2 changes that i made is : 1) Replace drawable with Bitmap
2) Replace timer with handler to schedule the image pulling from web.

Once again thanks for the replies.
androidrocks
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 24, 2009 8:47 am

Postby ClockworkAlchemist » Fri Mar 27, 2009 4:30 pm

Thanks for letting us know what it took to get it working! :D
ClockworkAlchemist
Junior Developer
Junior Developer
 
Posts: 14
Joined: Sun Feb 08, 2009 3:41 am
Location: Orange County, CA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 4 guests