============== 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();
}
}