import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends Activity {
private Button startDownloadBtn;
private ImageView resultImageView;
// private static final String IMAGE_URL = "http://e.hiphotos.baidu.com/image/pic/item/e850352ac65c10389f9a68c3b0119313b17e89cf.jpg";
private static final String IMAGE_URL = "http://d.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef9544b321fefaaf51f3de6652.jpg";
private DownloadThread downloadThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
startDownloadBtn = (Button) findViewById(R.id.startDownloadBtn);
startDownloadBtn.setOnClickListener(new mOnclickListener());
resultImageView = (ImageView) findViewById(R.id.resultImageView);
downloadThread = new DownloadThread(IMAGE_URL);
}
class mOnclickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//开启新线程下载
downloadThread.start();
}
}
class DownloadThread extends Thread{
private String imageurl;
private MyHandler myHandler = new MyHandler();
private Message msg = null;
public DownloadThread( String url) {
this.imageurl = url;
}
@Override
public void run() {
try {
URL url = new URL(imageurl);
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
Log.i("LENGTH", "LENGTH-----------------" + connection.getContentLength());
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//输入流关闭后说明已经获取了图像,这时候可以通过handler传送数据
msg = new Message();
msg.obj = bitmap;
//Handler 不能在另外一个线程任务里面实例化
myHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
//显示图片
Bitmap bitmap = (Bitmap)msg.obj;
resultImageView.setImageBitmap(bitmap);
super.handleMessage(msg);
}
}
}
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/resultImageView"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:id="@+id/startDownloadBtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center" />
当然最后不能忘记加入网络权限。
运行结果
运行有效,csdn暂时上传不了图片!