在main.xml上,我們需要放入ImageView、Button、EditText這樣三個控件,到時候,我們點擊按鈕的時候就可以從editText中獲取剛才我們獲取的地址字符串,并通過一系列操作獲取圖片數(shù)據(jù),再通過ImageView的setImageBitmap(bitmap)方法在界面上把圖片顯示出來。以下是詳細(xì)代碼,我一行一行給大家解讀一下
import java.io.ByteArrayOutputStream;
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.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class GetNetImageActivity extends Activity { //聲明三個控件
private TextView address;
private Button show;
private ImageView pic;
String path=”http://images.chinagate.cn/attachement/jpg/site1020/20100313/000cf1a487860d0567ba40.jpg”;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//實例化三個控件
address=(TextView)findViewById(R.id.textview);
show=(Button)findViewById(R.id.showImage);
pic=(ImageView)findViewById(R.id.iamgeview);
//設(shè)置button的點擊事件,一點擊就可以看到圖片
show.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{ //顯示出圖片需要位圖對象,即星號表明的這一行,而位圖對象需要用圖片數(shù)據(jù)來生成,而這個數(shù)據(jù)的獲得就是下面那個方法所指出的。
byte[] data =GetNetImageActivity.this. getData();
* Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
pic.setImageBitmap(bitmap);
}catch(Exception e){}
}
});
}//這個方法用于獲得圖片的二進制數(shù)據(jù)
private byte[] getData() throws Exception {//這個path就是我們的那張圖片的網(wǎng)絡(luò)地址,URL就像是我們使用瀏覽器時候的那個地址欄對象,他包裝了網(wǎng)址對象。
URL url = new URL(path);//調(diào)用URL對象的.openConnection() 返回一個HttpURLConnection 對象http
HttpURLConnection http = (HttpURLConnection) url.openConnection();//設(shè)置訪問延時時間秒數(shù),一般要在5秒以內(nèi)。否則程序可能會出現(xiàn)錯誤
http.setConnectTimeout(3000);//設(shè)置請求的方法,我們可以用GET方法,也可以用POST方法
http.setRequestMethod(“GET”); //調(diào)用http的關(guān)鍵方法:getInputStream(),這個getInputStream()方法用來獲取網(wǎng)絡(luò)資源文件的流對象,比如我們現(xiàn)在做的實驗室獲取圖片,那么getInputStream()方法就給我們圖片數(shù)據(jù)流
InputStream in = http.getInputStream();//我們剛才獲得了圖片數(shù)據(jù)流,還不能馬上轉(zhuǎn)化成我們要顯示的圖片,而是需要進行轉(zhuǎn)換,這個轉(zhuǎn)換方法封裝在getDta()這個方法里面,這個getData()方法需要我們傳入InputStream對象,并返回一個byte[] 對象,我們接下來看看這個getData()方法
byte[] b = GetNetImageActivity.this. getByte(in);
return b;
}
public byte[] getByte(InputStream i ) throws IOException{//字節(jié)數(shù)組輸出流,這個流就是起到緩沖的作用,
ByteArrayOutputStream out = new ByteArrayOutputStream();//構(gòu)造一個字節(jié)數(shù)組,這個字節(jié)數(shù)組的大小是1024個字節(jié),用于讓輸入流讀取數(shù)據(jù)到其中 byte[] buffer = new byte[1024];//這個len起到監(jiān)督貨物有沒有搬運完的作用。如果沒有搬運完,則len不會小于0,否則等于-1,當(dāng)?shù)扔?1的時候,就表達貨物搬運完成
int len = 0;//i.read()方法表示把流讀到字節(jié)數(shù)組中,返回的是讀取到的內(nèi)容的長度,在這個while里面我們判斷l(xiāng)en的數(shù)值大小,當(dāng)len不小于0的時候,我們就一直搬運貨物,直到小于0我們才不搬
while((len=i.read(buffer))!=-1){//在一邊讀的時候,我們就一邊寫,即調(diào)用輸出流的write()方法,把數(shù)據(jù)讀到內(nèi)存里面,讀多少,寫多少,意思是每次搬運buffer這樣容量的貨物。
out.write(buffer,0,len);
}
i.close();
return out.toByteArray();
}
}