简体   繁体   中英

Android downloading image

I am trying to download an image from the web server continuously. Below shown is the code

public class HMIActivity extends Activity implements Observer{

private ImageView imageView;

public HMIActivity()
{

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hmi);
}

@Override
protected void onStart()
{
    super.onStart();
    ImageDownloader imageDownloader = new ImageDownloader("http://192.168.5.109/hmi/img.jpg",this);
    Thread thread = new Thread(imageDownloader);
    thread.start();

     imageView = (ImageView) findViewById(R.id.imageView1);

}
    public void update(Observable obj,Object data)
{
    HTTPCommunicator httpCommunicator = (HTTPCommunicator)data;
          System.out.println("length----"+httpCommunicator.bytes.length);

}

Code for image downloader class

public class ImageDownloader implements Runnable{

private final String urlToDownloadImage;
private HMIActivity hmiActivity;
private int NTHREADS = 10;
ExecutorService executor = Executors.newFixedThreadPool(NTHREADS);

  public ImageDownloader(String url,HMIActivity _hmiActivty)
  {
    this.urlToDownloadImage = url;
    hmiActivity = _hmiActivty;
}

@Override
public void run(){

        while(true){
            HTTPCommunicator httpCommunicator = new HTTPCommunicator(this.urlToDownloadImage);
            httpCommunicator.addObserver(this.hmiActivity);
            executor.execute(httpCommunicator);
        }
    }
}

code for htppcommunicator class

public class HTTPCommunicator extends Observable implements Runnable {

String urlToDownloadImage;
private final int HTTP_CONNECTION_TIMEOUT = 10*1000;
public byte bytes[];

public HTTPCommunicator(String urlToDownloadImage)
{
    this.urlToDownloadImage = urlToDownloadImage;
}

@Override
public void run() {
    // TODO Auto-generated method stub

    try
        {
            URL url = new URL(urlToDownloadImage);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(HTTP_CONNECTION_TIMEOUT);
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();
            if(inputStream != null)
            {
                bytes = IOUtils.toByteArray(inputStream);

                setChanged();
                notifyObservers(this);
            }
        }
        catch(MalformedURLException _exception)
        {   
            Log.e("HTTPCommunicator","Malformed url exception");
        }
        catch(UnknownHostException _exception)
        {
            Log.e("HTTPCommunicator","Check Internet Connection!!!");
        }
    }
  }

This code runs fine only for 1 min then it throws the below shown exception

01-24 12:50:15.379: E/AndroidRuntime(912): FATAL EXCEPTION: pool-1-thread-7
01-24 12:50:15.379: E/AndroidRuntime(912): java.lang.OutOfMemoryError

I am not sure how to fix this issue. Please help me.

Well instead of manually downloading the images try the lazy loading of images.. For lazy loading u have plenty of options in android

1. UIL

2 Android Smart Image View

So you will not get out of memory exception..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM