Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解
Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解
看下實(shí)現(xiàn)效果圖:

1:androidmanifest.xml的內(nèi)容
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.capinftotech.image"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
注意訪問網(wǎng)絡(luò)中的數(shù)據(jù)需要添加android.permission.INTERNET權(quán)限
2:MainActivity的內(nèi)容
package cn.capinftotech.image;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.capinfotech.service.ImageService;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private EditText urlPath = null;
private Button button = null;
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
urlPath = (EditText)findViewById(R.id.urlpath);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String urlPathContent = urlPath.getText().toString();
try {
byte[] data = ImageService.getImage(urlPathContent);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); //生成位圖
imageView.setImageBitmap(bitmap); //顯示圖片
} catch (IOException e) {
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show(); //通知用戶連接超時(shí)信息
Log.i(TAG, e.toString());
}
}
});
}
}
3:ImageService類的內(nèi)容
package com.capinfotech.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.capinfotech.utils.StreamTool;
public class ImageService {
public static byte[] getImage(String path) throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET"); //設(shè)置請(qǐng)求方法為GET
conn.setReadTimeout(5*1000); //設(shè)置請(qǐng)求過時(shí)時(shí)間為5秒
InputStream inputStream = conn.getInputStream(); //通過輸入流獲得圖片數(shù)據(jù)
byte[] data = StreamTool.readInputStream(inputStream); //獲得圖片的二進(jìn)制數(shù)據(jù)
return data;
}
}
4:StreamTool的內(nèi)容
package com.capinfotech.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamTool {
/*
* 從數(shù)據(jù)流中獲得數(shù)據(jù)
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}
5:程序中用到的字符串資源strings.xml里的內(nèi)容
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">圖片瀏覽器</string> <string name="urlpath">網(wǎng)絡(luò)圖片地址</string> <string name="button">顯示</string> <string name="error">網(wǎng)絡(luò)連接超時(shí)</string> </resources>
6:程序布局文件main.xml的內(nèi)容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/urlpath" /> <EditText android:id="@+id/urlpath" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
以上使用Android 獲取網(wǎng)路圖片并顯示的實(shí)例,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能
- Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器
- Android 下載網(wǎng)絡(luò)圖片并顯示到本地
- android網(wǎng)絡(luò)圖片查看器簡(jiǎn)單實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法
- Android讀取本地或網(wǎng)絡(luò)圖片并轉(zhuǎn)換為Bitmap
- Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法
相關(guān)文章
Android ViewFlipper翻轉(zhuǎn)視圖使用詳解
這篇文章主要為大家詳細(xì)介紹了Android ViewFlipper翻轉(zhuǎn)視圖的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android實(shí)現(xiàn)在map上畫出路線的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在map上畫出路線的方法,較為詳細(xì)的分析了Android在map上繪制路線所涉及的map圖調(diào)用、畫筆的使用、頁面布局及權(quán)限控制的相關(guān)技巧,需要的朋友可以參考下2015-07-07
Android自定義View實(shí)現(xiàn)抖音飄動(dòng)紅心效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)抖音飄動(dòng)紅心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android OkHttp Post上傳文件并且攜帶參數(shù)實(shí)例詳解
這篇文章主要介紹了Android OkHttp Post上傳文件并且攜帶參數(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽詳解
本文主要介紹了Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽的方法步驟。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Android基礎(chǔ)知識(shí)之tween動(dòng)畫效果
Android基礎(chǔ)知識(shí)之tween動(dòng)畫效果,Android一共提供了兩種動(dòng)畫,這篇文章主要介紹了Android動(dòng)畫效果之tween動(dòng)畫,感興趣的小伙伴們可以參考一下2016-06-06
Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
由于listview的一些特性,剛開始寫這種需求的功能的時(shí)候都會(huì)碰到一些問題,重點(diǎn)就是存儲(chǔ)每個(gè)checkbox的狀態(tài)值,在這里分享出了完美解決方法:2013-06-06
Android 消息分發(fā)使用EventBus的實(shí)例詳解
這篇文章主要介紹了Android 消息分發(fā)使用EventBus的實(shí)例詳解的相關(guān)資料,在項(xiàng)目中用了許多Handler和broadcast導(dǎo)致代碼冗余,顯得雜亂無章,這里使用EventBus來實(shí)現(xiàn)相同的功能,需要的朋友可以參考下2017-07-07

