Android獲取網(wǎng)絡(luò)圖片并顯示的方法
本文實(shí)例為大家分享了Android獲取網(wǎng)絡(luò)圖片并顯示的具體代碼,供大家參考,具體內(nèi)容如下
使用 HttpURLConnection 獲得連接,再使用 InputStream 獲得圖片的數(shù)據(jù)流,通過 BitmapFactory 將數(shù)據(jù)流轉(zhuǎn)換為 Bitmap,再將 Bitmap 通過線程的 Message 發(fā)送出去,Handler 接收到消息就會(huì)通知 ImageView 顯示出來。

記得要在manifest文件中添加 < uses-permission android:name=”android.permission.INTERNET” />上網(wǎng)權(quán)限,不然無法顯示圖片。
工程文件結(jié)構(gòu):

布局文件中就一個(gè) ImageView 用來顯示圖片,一個(gè) Button 用來獲取圖片。
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
String url = "http://i4.buimg.com/dccba6282641a9e0.jpg";
//String textURL = "http://192.168.1.104:8080/add.jsp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bmp = getURLimage(url);
Message msg = new Message();
msg.what = 0;
msg.obj = bmp;
System.out.println("000");
handle.sendMessage(msg);
}
}).start();
}
});
}
//在消息隊(duì)列中實(shí)現(xiàn)對(duì)控件的更改
private Handler handle = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
System.out.println("111");
Bitmap bmp=(Bitmap)msg.obj;
imageView.setImageBitmap(bmp);
break;
}
};
};
//加載圖片
public Bitmap getURLimage(String url) {
Bitmap bmp = null;
try {
URL myurl = new URL(url);
// 獲得連接
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
conn.setConnectTimeout(6000);//設(shè)置超時(shí)
conn.setDoInput(true);
conn.setUseCaches(false);//不緩存
conn.connect();
InputStream is = conn.getInputStream();//獲得圖片的數(shù)據(jù)流
bmp = BitmapFactory.decodeStream(is);//讀取圖像數(shù)據(jù)
//讀取文本數(shù)據(jù)
//byte[] buffer = new byte[100];
//inputStream.read(buffer);
//text = new String(buffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用網(wǎng)絡(luò)獲取定位的方法
- Android8.1原生系統(tǒng)網(wǎng)絡(luò)感嘆號(hào)消除的方法
- 詳解Android 利用Iptables實(shí)現(xiàn)網(wǎng)絡(luò)黑白名單(防火墻)
- Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作
- android實(shí)現(xiàn)okHttp的get和post請(qǐng)求的簡(jiǎn)單封裝與使用
- Android中okhttp3使用詳解
- 詳解Android中OkHttp3的例子和在子線程更新UI線程的方法
- 探究Android客戶端網(wǎng)絡(luò)預(yù)連接優(yōu)化機(jī)制
相關(guān)文章
淺析Android Studio 3.0 升級(jí)各種坑(推薦)
本文是小編給大家收藏整理的關(guān)于Android Studio 3.0 升級(jí)后遇到的一些坑,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-11-11
Flutter使用?input?chip?標(biāo)簽組件示例詳解
這篇文章主要為大家介紹了Flutter使用?input?chip?標(biāo)簽組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
三種Android單擊事件onclick的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了三種Android單擊事件onclick的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05
Android平臺(tái)中實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)的5種方式
這篇文章主要為大家分享了介紹了Android平臺(tái)中實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)技術(shù)的5種方式,供大家學(xué)習(xí),感興趣的小伙伴們可以參考一下2016-06-06
Android 對(duì)話框(Dialog)大全詳解及示例代碼
本文主要介紹Android 對(duì)話框的知識(shí),這里整理了詳細(xì)資料及實(shí)現(xiàn)示例代碼及實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-09-09
Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(TreeRecyclerView)
RecyclerView出現(xiàn)已經(jīng)有一段時(shí)間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(TreeRecyclerView)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下。2017-05-05
完美解決EditText和ScrollView的滾動(dòng)沖突(下)
這篇文章再次為大家詳細(xì)介紹了完美解決EditText和ScrollView滾動(dòng)沖突的方法,感興趣的小伙伴們可以參考一下2016-06-06

