ImageText.java
package com.example.myasynctask;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class ImageText extends Activity {
private ImageView mImageView;
private ProgressBar mprogressBar;
private static String URL = "http://img.my.csdn.net/uploads/201504/12/1428806103_9476.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
mImageView = (ImageView) findViewById(R.id.image);
mprogressBar = (ProgressBar) findViewById(R.id.progressbars);
Toast.makeText(this, "呵呵", 1).show();
new MyAsyncTask().execute(URL);
}
class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mprogressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
mprogressBar.setVisibility(View.GONE);
mImageView.setImageBitmap(bitmap);
}
@Override
protected Bitmap doInBackground(String... params) {
String url=params[0];
Bitmap bitmap=null;
URLConnection connection;
InputStream is;
try {
connection=new URL(url).openConnection();
is=connection.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
Thread.sleep(3000);
bitmap=BitmapFactory.decodeStream(bis);
is.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
}
}
image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressbars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
不知道哪里错了,我toast在new MyAsyncTask().execute(URL);就不打印了