Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實例詳解
Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實例詳解
看下實現(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(); //通知用戶連接超時信息
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è)置請求方法為GET
conn.setReadTimeout(5*1000); //設(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ò)連接超時</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)路圖片并顯示的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
詳解Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密
對于已經(jīng)ROOT的手機(jī)來說的沒有任何安全性可以,一旦被利用將會導(dǎo)致數(shù)據(jù)庫數(shù)據(jù)的泄漏,本篇文章主要介紹了Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密,具有一定的參考價值,有需要的可以了解一下。2016-12-12
Android實戰(zhàn)打飛機(jī)游戲之怪物(敵機(jī))類的實現(xiàn)(4)
這篇文章主要為大家詳細(xì)介紹了Android實戰(zhàn)打飛機(jī)游戲之怪物(敵機(jī))類的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android入門之Glide顯示網(wǎng)絡(luò)圖片高版本的使用詳解
這篇文章主要為大家詳細(xì)介紹了Android中Glide顯示網(wǎng)絡(luò)圖片高版本的使用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02
Android Listview中顯示不同的視圖布局詳解及實例代碼
這篇文章主要介紹了Android Listview中顯示不同的視圖布局詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android IPC機(jī)制利用Messenger實現(xiàn)跨進(jìn)程通信
這篇文章主要介紹了Android IPC機(jī)制中 Messager 實現(xiàn)跨進(jìn)程通信的知識,對Android學(xué)習(xí)通信知識非常重要,需要的同學(xué)可以參考下2016-07-07
android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法
以前倒是沒有注意listview的getView會重復(fù)執(zhí)行多次,在測試的時候去斷點跟蹤,發(fā)現(xiàn)同一條數(shù)據(jù)不斷的重復(fù)執(zhí)行,下面與大家分享下正確的解決方法,希望對你有所幫助2013-06-06

