package com.iaiai;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hilary.utils.AsyncImageLoader;

/**
*
* <p>
* Title: Main.java
* </p>
* <p>

* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-10-26
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class Main extends Activity {
Button btn2;
ImageView imge;
Thread thread = null;
Runnable runnable = null;
TextView textView = null;
Drawable dra = null;
private AsyncImageLoader asyncImageLoader;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
asyncImageLoader = AsyncImageLoader.getAsyncImageLoader();
btn2 = (Button) findViewById(R.id.btn2);
textView = (TextView) findViewById(R.id.show_tv);
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Handler handler = new Handler();
// handler.post(runable);
textView.setText(Html.fromHtml( _readFile("/sdcard/test2.txt"), imgGetter, null));
}
});
}

public String _readFile(String str) {
StringBuffer sb = new StringBuffer();
File file = new File(str);
try {
FileReader is = new FileReader(file);
while (is.ready()) {
int c = is.read();
sb.append((char) c);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

ImageGetter imgGetter = new Html.ImageGetter() {

@Override
public Drawable getDrawable(String source) {
System.out.println("***" + source);
//异步加载图片
// Drawable drawable = asyncImageLoader.loadDrawable(
// source, new ImageCallback() {
//
// @Override
// public void imageLoaded(Drawable imageDrawable,
// String imageUrl) {
// if (imageDrawable == null) {
// } else {
// imageDrawable.setBounds(0, 0, imageDrawable.getIntrinsicWidth(), imageDrawable
// .getIntrinsicHeight());
// }
// dra = imageDrawable;
// }
// });
// if(source.equals("1")){
// drawable = Main.this.getResources().getDrawable(R.drawable.aa);
// } else if (source.equals("2")){
// drawable = Main.this.getResources().getDrawable(R.drawable.b);
// } else {
// drawable = Main.this.getResources().getDrawable(R.drawable.icon);
// }
URL url;
Drawable drawable = null;
try {
url = new URL(source);
drawable = Drawable.createFromStream(url.openStream(), "");
} catch (Exception e) {
return null;
}

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}

};

}