您的位置:首页 > 其它

点击按钮弹出对话框 下载图片

2017-08-15 15:16 357 查看
package com.bwie.rokao_15;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

private LinearLayout ll;
private ProgressBar pb;
private TextView tv;
private ImageView iv;
private String url="http://pic62.nipic.com/file/20150319/12632424_132215178296_2.jpg";
private ProgressDialog pb1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
}

/**
* 点击 异步加载图片
*/
public void onclick(View v){

new MyAsyncTask().execute(url);
}

class MyAsyncTask extends AsyncTask<String,Integer,Bitmap>{
/**
* 请求前
*/
@Override
protected void onPreExecute() {

//显示
//ll.setVisibility(View.VISIBLE);
pb1 = new ProgressDialog(MainActivity.this);
pb1.setIcon(R.mipmap.ic_launcher);
pb1.setTitle("更新进度");
pb1.setMessage("请稍候");
pb1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pb1.setCancelable(false);
pb1.setCanceledOnTouchOutside(false);
pb1.setMax(100);
pb1.setButton(DialogInterface.BUTTON_POSITIVE, "暂停", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
pb1.show();
}

/**
* 请求中
* @param params
* @return
*/
@Override
protected Bitmap doInBackground(String... params) {
try{
URL url=new URL(params[0]);
//请求图片
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.connect();

//获取总长度
int file_length=connection.getContentLength();

if(200==connection.getResponseCode()){

//当前长度
int total_length=0;
//当前长度int值
int value=0;
//成功
InputStream in=connection.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len;
byte[] b=new byte[1024];
while((len=in.read(b))!=-1){
//依次长度
total_length+=len;
value= (int) ((float)total_length/file_length*100);

//更新
publishProgress(value);
baos.write(b,0,len);
}
//返回结果
return BitmapFactory.decodeByteArray(baos.toByteArray(),0,baos.size());
}

}catch (Exception e){
e.printStackTrace();
}
return null;
}

/**
* 请求进度
* @param values
*/
@Override
protected void onProgressUpdate(Integer... values) {

//pb.setProgress(values[0]);
tv.setText(values[0]+"%");//一定不能是int类型呀
pb1.setProgress(values[0]);
}

/**
* 请求结果
* @param bitmap
*/
@Override
protected void onPostExecute(Bitmap bitmap) {

ll.setVisibility(View.GONE);
//完成
iv.setImageBitmap(bitmap);
pb1.dismiss();
}
}

private void initView() {
ll= (LinearLayout) findViewById(R.id.ll);
pb= (ProgressBar) findViewById(R.id.pb);
tv= (TextView) findViewById(R.id.tv);
iv= (ImageView) findViewById(R.id.iv);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: