package pkg.DestMsgs;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

public class ReadImageThread implements Runnable {

	Thread t;
	static URL urlImage = null;
	int msgId;

	public ReadImageThread(int pMsgId) {
		// Create a new, second thread
		msgId = pMsgId;
		t = new Thread(this, "Image Thread");
		// System.out.println("Child thread: " + t);
		t.start(); // Start the thread
		System.out.println(" thrd c'tor " + msgId);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println(" thrd run " + msgId);
		Bitmap bm = null;
		try {
			urlImage = new URL(Constants.ReadURL + "?msgid=" + msgId);
		} catch (MalformedURLException e) {
			e.printStackTrace(System.out);
			// Log.e(e.getMessage());
		}

		if (urlImage != null) {
			try {
				HttpURLConnection urlConn = (HttpURLConnection) urlImage
						.openConnection();
				InputStream ins = urlConn.getInputStream();

				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 8;
				Rect re = new Rect(0, 0, 320, 320);
				bm = BitmapFactory.decodeStream(ins, re, options);
				ReadMessages.hmImages.put(msgId, bm);
				ins.close();
				urlConn.disconnect();

			} catch (IOException e) {
				e.printStackTrace(System.out);
				// Log.e(e.getMessage());
			}

		}

	}

}
