点击按钮弹出对话框 下载图片
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); } }
相关文章推荐
- 点击按钮弹出下载对话框
- Java点击下载按钮弹出下载文件对话框
- 事件2:点击一个按钮,弹出一个对话框
- asp.net点击按钮下载图片而不是打开图片
- 点击JSP页面后弹出“下载”对话框问题
- C#调用迅雷的时候 自动模拟点击”下载按钮” 关闭弹出窗口
- asp.net 点击一个按钮弹出对话框选择文件然后选择文件完成之后就自动上传文件 input(file)
- 输入框进行判断验证(弹出对话框,点击按钮查询并隐藏)
- Excel点击按钮弹出对话框
- 点击删除按钮弹出对话框提示是否删除
- uploadify在ie9下点击按钮后,不弹出浏览文件对话框的问题?
- asp.net点击按钮下载图片而不是打开图片
- dojo点击按钮弹出对话框
- iPad点击按钮弹出alert对话框崩溃
- C#+Emgucv通过点击按钮弹出选择文件对话框
- 事件3:点击一个按钮,弹出一个对话框
- ProgressDialog 通过点击按钮,弹出一个对话框
- 点击UpdatePanel中的按钮弹出javascript对话框
- 点击按钮,弹出对话框
- MFC 点击按钮,弹出另一个对话框