Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
我們的應(yīng)用或多或少都會(huì)從網(wǎng)絡(luò)獲取圖片數(shù)據(jù)然后進(jìn)行顯示,下面就將實(shí)現(xiàn)一個(gè)這樣的例子,獲取網(wǎng)絡(luò)中的圖片!
首先:我們來看一下效果圖
界面中有三個(gè)控件,一個(gè)EditText,一個(gè)Button,一個(gè)ImageView
1、下面是具體布局文件
<EditText
android:id="@+id/picturepagh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查看" />
<ImageButton
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="200px" />
2、在MainActivity中進(jìn)行圖片圖示代碼編寫
public class MainActivity extends Activity {
private Button btn;
private EditText path;
private ImageView imgview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
path = (EditText) findViewById(R.id.picturepagh);
imgview = (ImageView) findViewById(R.id.imageView);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("CLICK", ((Button) v).getText().toString());
new Thread(runa).start();
}
});
}
public void setView() {
String picturepath = path.getText().toString();
byte[] data = null;
try {
data = ImageService.getImage(picturepath);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// BitmapFactory:圖片工廠!
Looper.prepare();// 必須調(diào)用此方法,要不然會(huì)報(bào)錯(cuò)
Message msg = new Message();
msg.what = 0;
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "獲取圖片錯(cuò)誤", 1).show();
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
updateImageView((Bitmap) msg.obj);
}
}
};
private Runnable runa = new Runnable() {
@Override
public void run() {
setView();
}
};
private void updateImageView(Bitmap bm) {
imgview.setImageBitmap(bm);
}
}
3、添加一個(gè)ImageService圖片服務(wù)類,里面包含一個(gè)獲取網(wǎng)絡(luò)數(shù)據(jù)的方法;
public class ImageService {
// 獲取網(wǎng)絡(luò)圖片的數(shù)據(jù)
public static byte[] getImage(String picturepath) throws Exception {
URL url = new URL(picturepath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基于http協(xié)議的連接對(duì)象
conn.setConnectTimeout(10);// 10秒;
conn.setRequestMethod("GET");// 大寫
if (conn.getResponseCode() == 200) {
InputStream ins = conn.getInputStream();
return StreamTool.read(ins);
}
return null;
}
}
4、添加一個(gè)流處理工作類StreamTool
public class StreamTool {
public static byte[] read(InputStream ins) throws Exception {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = ins.read(buffer)) > -1) {
outstream.write(buffer, 0, length);
}
outstream.close();
return outstream.toByteArray();
}
}
5、大功告成?NO,還要添加網(wǎng)絡(luò)訪問權(quán)限: <uses-permission android:name="android.permission.INTERNET" />
OK,運(yùn)行程序!
- Android讀取本地或網(wǎng)絡(luò)圖片并轉(zhuǎn)換為Bitmap
- Android 異步獲取網(wǎng)絡(luò)圖片并處理導(dǎo)致內(nèi)存溢出問題解決方法
- Android顯示網(wǎng)絡(luò)圖片實(shí)例
- Android 下載網(wǎng)絡(luò)圖片并顯示到本地
- 簡(jiǎn)單實(shí)現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地
- Android使用線程獲取網(wǎng)絡(luò)圖片的方法
- 在Android的應(yīng)用中實(shí)現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法
- Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能
- Android 讀取sdcard上的圖片實(shí)例(必看)
- Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載
- Android開發(fā)實(shí)現(xiàn)加載網(wǎng)絡(luò)圖片并下載至本地SdCard的方法
相關(guān)文章
android連續(xù)拖動(dòng)導(dǎo)致掛起的解決方法
本文給大家分享的是在安卓的項(xiàng)目開發(fā)中遇到連續(xù)拖動(dòng)對(duì)象,導(dǎo)致掛起的問題的解決方法,也是經(jīng)過很多網(wǎng)友的提示,最終才找到解決方法,這里記錄一下,分享給大家。2015-05-05android實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android Menu半透明效果的開發(fā)實(shí)例
這篇文章主要介紹了Android Menu半透明效果方法的相關(guān)資料,需要的朋友可以參考下2016-09-09Android?Flutter使用本地?cái)?shù)據(jù)庫編寫備忘錄應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android?Flutter如何使用本地?cái)?shù)據(jù)庫實(shí)現(xiàn)編寫簡(jiǎn)單的備忘錄應(yīng)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03android 仿QQ動(dòng)態(tài)背景、視頻背景的示例代碼
本篇文章主要介紹了android 仿QQ動(dòng)態(tài)背景、視頻背景的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Android 2d游戲開發(fā)之貪吃蛇基于surfaceview
這篇文章主要介紹了Android 2d游戲開發(fā)基于surfaceview的貪吃蛇,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Android中的Notification機(jī)制深入理解
這篇文章主要給大家介紹了關(guān)于Android中Notification機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的方法
這篇文章主要介紹了Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05