Android 图片下载并显示
来源:互联网 发布:笔记本触摸手写软件 编辑:程序博客网 时间:2024/05/29 19:50
package com.test.activity;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import java.net.URLConnection;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Window;import android.widget.ImageView;import com.test.R;public class BitmapActivity extends Activity {private ImageView imageView ;private final static String IMAGEURL = "https://gss1.bdstatic.com/5eN1dDebRNRTm2_p8IuM_a/res/img/logo/logo201509091.png";private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {File file = (File)msg.obj;Bitmap bitmap = null;try {bitmap = BitmapFactory.decodeStream(new FileInputStream(file));} catch (FileNotFoundException e) {e.printStackTrace();}imageView.setImageBitmap(bitmap);}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.bitmap);imageView = (ImageView)findViewById(R.id.imageView);new Thread(){@Overridepublic void run() {Message message = handler.obtainMessage();message.obj = downloadFile(IMAGEURL);message.sendToTarget();}}.start();}//下载图片private File downloadFile(String urlStr){URL url = null ;InputStream input = null ;OutputStream output = null ;File file = null ;try {url = new URL(urlStr);URLConnection urlConnection = url.openConnection();urlConnection.setConnectTimeout(5*1000); input = urlConnection.getInputStream();file = File.createTempFile("xxxx", "jpg");output = new FileOutputStream(file) ; byte[] byt = new byte[1024];int length = 0;// 开始读取 while ((length = input.read(byt)) != -1) { output.write(byt, 0, length); } input.close();output.close();System.out.println("下载完成");} catch (Exception e) {e.printStackTrace();System.out.println("下载出错");}return file ;}}
0 0
- android 图片下载并显示
- Android 图片下载并显示
- android图片下载并保存
- Android Http基础:图片下载并显示和WebView的应用
- Android多线程方式处理图片下载及显示
- [Android开发]Gif图片下载和显示
- Android-仿新浪微博图片下载进度显示
- 图片下载保存显示
- 读取网络图片下载到SD卡并显示在ImageView中
- 网络图片下载并压缩保存
- 网络图片下载并保存本地
- android图片下载工具类
- android 图片下载 类
- android 图片下载、放大缩小
- Android实现图片下载
- Android图片下载机制
- android图片下载器
- Android中的Fresco图片下载
- 调用WSDL文件的两种方法(非动态和动态)
- Linux 下三种方式设置环境变量
- Spring Quartz 参数说明
- android ksoap2调用cxf webservice
- Hadoop学习笔记(7)-HIVE安装
- Android 图片下载并显示
- c++ 之多态篇(下)----虚表,多态调用
- JS原型链中 _proto_和prototype及constructor的关系
- FillMode详解
- 2017sdut省赛选拔(1)--poj2431(贪心)
- Android 日志打印功能及项目
- android 5.1状态栏添加截屏功能
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
- android日历例子