獲取伺服器Url圖片資源,並顯示在ImageView中 Android
在ImageView_test.xml檔案中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/lookView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="檢視圖片"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
在Activity中,
/**
*
*/
package com.example.webviewdemo;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
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.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* <p>Title: Foundation SysLog </p>
* <p>Description:
* This is a change object in SysLog
* </p>
* <p>@author: subh </p>
* <p>Copyright: Copyright (c) 2012 </p>
* <p>Company: FFCS Co., Ltd. </p>
* <p>Create Time: 2013年8月9日 </p>
* <p>Update Time: </p>
* <p>Updater: </p>
* <p>Update Comments: </p>
*/
public class ImageViewTest extends Activity {
private Button btnLook;
private ImageView ivImageView;
private String urlPath=null;
private Bitmap bitmap;
private MyHandle mHandle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自動生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview_test);
initComponent();
initData();
setLisenter();
}
public void initComponent(){
btnLook=(Button)findViewById(R.id.lookView);
ivImageView=(ImageView)findViewById(R.id.imageView);
mHandle=new MyHandle();
}
public void initData(){
urlPath=getIntent().getStringExtra("urlFile");
}
public void setLisenter(){
btnLook.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
DownloadImageRunner runnable=new DownloadImageRunner();
new Thread(runnable).start(); //呼叫執行緒
}
});
}
//建立執行緒
private class DownloadImageRunner implements Runnable {
public void run() {
Message msg = new Message();
msg.what = 1;
try {
URL url;
url = new URL(urlPath);
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection(); //建立連結
conn.setConnectTimeout(6000); //超時設定
conn.setDoInput(true);
conn.setUseCaches(false); //不設定本地快取
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is); //將流轉為Bitmap
is.close(); //關閉流
} catch (IOException e) {
msg.what = 0;
e.printStackTrace();
} catch (Exception e) {
msg.what = 0;
e.printStackTrace();
}
mHandle.sendMessage(msg); //傳送handle資訊請求
}
}
private class MyHandle extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO 自動生成的方法存根
super.handleMessage(msg);
if (msg.what==1) {
ivImageView.setImageBitmap(bitmap); //顯示ImageView圖片
} else {
Toast.makeText(ImageViewTest.this, "獲取伺服器圖片失敗", Toast.LENGTH_SHORT).show();
}
}
}
}