activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:src="@drawable/ic_launcher" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:text="TextView" />
</RelativeLayout>
DownLoadUtil.java
public class DownloadUtil extends AsyncTask<String, Integer, String> {
private ImageView image;
private TextView view;
private ProgressBar bar;
private Context context;
private URL url;
private DataInputStream dis;
private URLConnection connection;
private ByteArrayOutputStream baos;
private int progress_max_value;
private int progress_current_value;
private File file;
private FileOutputStream fos;
public DownloadUtil(ImageView image, TextView view, ProgressBar bar,
Context context) {
super();
this.image = image;
this.view = view;
this.bar = bar;
this.context = context;
}
// 初始化组件,进度条
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
try {
URL url = new URL(
"http://imgt8.bdstatic.com/it/u=2,961490168&fm=19&gp=0.jpg");
connection = url.openConnection();
dis = new DataInputStream(connection.getInputStream());
baos = new ByteArrayOutputStream();
// 得到图片的大小
progress_max_value = connection.getContentLength();
bar.setMax(progress_max_value);
// 将图片存放在sd卡内
file = new File("mnt/sdcard/yaya.jpg");
// 判断file文件是否存在
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
byte[] b = new byte[1024 * 4];
int len = 0;
try {
while ((len = dis.read(b)) != -1) {
baos.write(b, 0, len);
fos.write(b, 0, len);
publishProgress(len);
}
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return baos.toString();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progress_current_value += values[0];
bar.setProgress(progress_current_value);
view.setText("当前进度:"
+ ((progress_current_value * 100) / progress_max_value) + "%");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
byte[] re = baos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(re, 0, re.length);
image.setImageBitmap(bitmap);
}
}
MainActivity.java
public class MainActivity extends Activity {
private ImageView image;
private TextView view;
private ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initDownLoad();
}
private void initDownLoad() {
DownloadUtil downloadUtil = new DownloadUtil(image, view, bar,
MainActivity.this);
downloadUtil.execute(null);
}
private void initView() {
image = (ImageView) findViewById(R.id.imageView1);
view = (TextView) findViewById(R.id.textView1);
bar = (ProgressBar) findViewById(R.id.progressBar1);
}
}