views:

33

answers:

0

Hi!

I'm stuck with a simple program. I want to grab images from an external server every x seconds and display it on my android. My problem is that the display doesn't update. When I put everything in one class it works perfectly. The code beneath compiles and runs without any error but there must be something I missed because the screen stays black. Any suggestion what I forgot? Thanks a lot! Durin

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.cam;

import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author and
 */
public class CammCam extends Activity {

    public static Drawable drawable;
    public static ImageView imgView;

    public int seconds = 10;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Timer timer = new Timer();
        imgView = (ImageView) findViewById(R.id.ImageView01);
        timer.schedule(new WebcamUpdate(), seconds * 1000);
    }

    public static ImageView getImgView(){
        return imgView;
    }

     public static Drawable getDrawable(){
        return drawable;
    }

}

class WebcamUpdate extends TimerTask {
    String inputUrl = "http://my-url-.com";

    public void run() {
           Drawable drawable=CammCam.getDrawable();
           ImageView imgView=CammCam.getImgView();
           Log.d(this.toString(),"Updating Cam...");
           drawable=LoadImageFromWebOperations(inputUrl);
           imgView.setImageDrawable(drawable);
    }


    private Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            System.out.println("Exc=" + e);
            return null;
        }
    }

  }