12月1日备课,下载图片,显示进度
来源:互联网 发布:手机直播聊天源码系统 编辑:程序博客网 时间:2024/06/11 15:55
package com.example.progressnotification;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image;private int sum;private boolean isSum = true;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image);}public void btnClick(View view) { new DownloadImage() .execute("http://sony.it168.com/data/attachment/forum/201410/20/222208nct678ofnorcwclf.jpg");}class DownloadImage extends AsyncTask<String, Integer, Bitmap> { NotificationCompat.Builder builder = new NotificationCompat.Builder( MainActivity.this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("下载图片").setContentText("请稍后"); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int totalNum = 0; private byte[] imageByteArray; @Override protected Bitmap doInBackground(String... params) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(params[0]); ByteArrayOutputStream baos = null; try { HttpResponse res = client.execute(get); if (res.getStatusLine().getStatusCode() == 200) { HttpEntity entity = res.getEntity(); InputStream is = entity.getContent(); // 获得最大值 sum = (int) entity.getContentLength(); publishProgress(sum); baos = new ByteArrayOutputStream(); byte[] b = new byte[100]; int a = 0; int total = 0; while ((a = is.read(b)) != -1) { total += a; // 更新当前进度 publishProgress(total); baos.write(b, 0, a); } Bitmap bitmap = BitmapFactory.decodeByteArray( baos.toByteArray(), 0, baos.toByteArray().length); return bitmap; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); image.setImageBitmap(result); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); if (isSum) { totalNum = values[0]; isSum = false; } else { builder.setProgress(totalNum,values[0], false); manager.notify(1, builder.build()); if (values[0] == totalNum) { builder.setProgress(0, 0, false); builder.setContentText("下载完毕"); manager.notify(1, builder.build()); } } }}
}
- 12月1日备课,下载图片,显示进度
- 12月1日备课-Fragment+WebView展示详情页
- AsyncTask下载图片、显示进度
- iOS下载图片并显示进度
- 安卓下载网络图片显示进度
- AsyncTask下载网络图片并显示下载进度
- LASIImageView - 显示进度指示并异步下载图片
- 2016年1月28日学习进度
- NSURLConnection下载显示进度
- python3 显示下载进度
- okhttputils显示下载进度
- Android中从网上下载一张图片显示进度并显示下载好的图片
- 运用AsyncTask下载图片并显示下载进度,下载完成后显示
- 1月12日
- 1月12日
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Android开发 下载显示进度
- Android 设置EditText光标颜色及粗细
- MySQL 慢日志解析推送
- H5之11__GeoLocation 地理定位
- 我要做一名专业的TC/CF翻译、、、
- 如何在java程序中调用linux命令或者shell脚本
- 12月1日备课,下载图片,显示进度
- 用函数调用和指针写strcat函数
- HADOOP单机模式安装
- 我所感受到的世界
- Session 1: From Virtualization to Cloud Computing
- C++二叉排序树之删除结点
- EBS MO_GLOBAL包的分析
- source insight遇到__attribute__解析不到函数
- 告别NullPointerException:Scala中的Option