android 图片下载并显示
来源:互联网 发布:相册制作模板软件 编辑:程序博客网 时间:2024/06/06 18:03
============== qr_code_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="98dp"
android:layout_marginTop="140dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
============ QrCodeActivity
package com.sls.android_now;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import android.annotation.SuppressLint;
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.widget.ImageView;
import android.widget.Toast;
/**
*
* @Description:
* @author
* @Date:
*
*/
public class QrCodeActivity extends Activity {
private ImageView imageView;
private Bitmap bitmap;
private QrCodeHandler qrCodeHandler;
private static final int LONG_DELAY = 1000; // 1 seconds
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qr_code_view);
imageView = (ImageView) findViewById(R.id.imgSource);
qrCodeHandler = new QrCodeHandler(QrCodeActivity.this);
QrCodeThread qrCodeThread = new QrCodeThread();
new Thread(qrCodeThread).start();
}
@SuppressLint("HandlerLeak")
class QrCodeHandler extends Handler {
WeakReference<QrCodeActivity> actList;
QrCodeHandler(QrCodeActivity activity) {
this.actList = new WeakReference<QrCodeActivity>(activity);
}
// 子类必须重写此方法,接受数据
@Override
public void handleMessage(Message msg) {
QrCodeActivity activity = actList.get();
byte[] pic = msg.getData().getByteArray("pic");
try {
if (pic != null) {
activity.bitmap = BitmapFactory.decodeByteArray(pic, 0,
pic.length);// bitmap
activity.imageView.setImageBitmap(activity.bitmap);// display
} else {
Toast.makeText(QrCodeActivity.this, "Image error!",
LONG_DELAY).show();
}
} catch (Exception e) {
Toast.makeText(QrCodeActivity.this, "Newwork error!",
LONG_DELAY).show();
e.printStackTrace();
}
}
}
class QrCodeThread implements Runnable {
@Override
public void run() {
String filePath = "http://www2.slswd.cn/Res/images2013/login/t_04.jpg";
try {
// 取得的是byte数组, 从byte数组生成bitmap
byte[] pic = getImage(filePath);
Message msg = new Message();
Bundle dataBundle = new Bundle();
dataBundle.putByteArray("pic", pic);
msg.setData(dataBundle);
qrCodeHandler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public byte[] getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(inStream);
}
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
}
- android 图片下载并显示
- Android 图片下载并显示
- android图片下载并保存
- Android Http基础:图片下载并显示和WebView的应用
- Android多线程方式处理图片下载及显示
- [Android开发]Gif图片下载和显示
- Android-仿新浪微博图片下载进度显示
- 图片下载保存显示
- 读取网络图片下载到SD卡并显示在ImageView中
- 网络图片下载并压缩保存
- 网络图片下载并保存本地
- android图片下载工具类
- android 图片下载 类
- android 图片下载、放大缩小
- Android实现图片下载
- Android图片下载机制
- android图片下载器
- Android中的Fresco图片下载
- Java Session超时设置
- append,appendTo和prepend #1daae2
- 导入项目名称重复
- MyEclipse10 中的两种FreeMarker插件的安装与配置
- MyEclipse常用插件使用教程
- android 图片下载并显示
- Myeclipse10下载,安装,破解,插件,优化介绍
- HTTP错误大全 404 200 501 502 505
- c#等比例缩放、压缩gif、动图文件
- html 输入框 只能输入数字 只能输入字母数字组合
- 用标准C编写COM(二)COM in plain C,Part2
- Eclipse安装FindBug
- Eclipse中的checkstyle插件
- Tomcat之容器