Android編程獲取圖片數(shù)據(jù)的方法詳解
本文實(shí)例講述了Android編程獲取圖片數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
網(wǎng)絡(luò)的訪問在我們?nèi)粘I钪刑匾耍绻麤]有網(wǎng)絡(luò)我們的生活將會(huì)是什么樣子呢?Android手機(jī)和瀏覽器也是一樣的,也可以通過(guò)網(wǎng)絡(luò)通訊獲取數(shù)據(jù),如調(diào)用webservice,EJB等。下面就通過(guò)一個(gè)小例子從網(wǎng)絡(luò)獲取一幅圖片并顯示在手機(jī)上,開發(fā)中將會(huì)使用到一個(gè)新的組件ImageView.
1. 寫一個(gè)用來(lái)處理字節(jié)流的工具類
package org.lxh.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTool {
public static byte[] readInputStream(InputStream in) throws Exception{
int len=0;
byte buf[]=new byte[1024];
ByteArrayOutputStream out=new ByteArrayOutputStream();
while((len=in.read(buf))!=-1){
out.write(buf,0,len); //把數(shù)據(jù)寫入內(nèi)存
}
out.close(); //關(guān)閉內(nèi)存輸出流
return out.toByteArray(); //把內(nèi)存輸出流轉(zhuǎn)換成byte數(shù)組
}
}
2. 寫一個(gè)得到圖片byte數(shù)組的service類
package org.lxh.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.lxh.util.StreamTool;
import android.util.Log;
public class WebService {
public static byte[] getImage(String path){
URL url;
byte[] b=null;
try {
url = new URL(path); //設(shè)置URL
HttpURLConnection con;
con = (HttpURLConnection)url.openConnection(); //打開連接
con.setRequestMethod("GET"); //設(shè)置請(qǐng)求方法
//設(shè)置連接超時(shí)時(shí)間為5s
con.setConnectTimeout(5000);
InputStream in=con.getInputStream(); //取得字節(jié)輸入流
b=StreamTool.readInputStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return b; //返回byte數(shù)組
}
}
3. 寫一個(gè)用戶操作界面
<?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/hello" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/picaddress" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="http://www.desk9.com/Desk9Image/21/Desk9_21_1690_35790_S.jpg" android:id="@+id/imageaddress" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/look" android:id="@+id/button" /> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/image"/> </LinearLayout>
4. 寫一個(gè)activity類
package org.lxh.net;
import org.lxh.service.WebService;
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;
public class NetActivity extends Activity {
private EditText picaddress;
private Button button;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)this.findViewById(R.id.button);
imageView=(ImageView)this.findViewById(R.id.image);
picaddress=(EditText)this.findViewById(R.id.imageaddress);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String address=picaddress.getText().toString();
try {
byte[] data=WebService.getImage(address); //得到圖片的輸入流
//二進(jìn)制數(shù)據(jù)生成位圖
Bitmap bit=BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bit);
} catch (Exception e) {
Log.e("NetActivity", e.toString());
Toast.makeText(NetActivity.this, R.string.error, 1).show();
}
}
});
}
}
5. 添加網(wǎng)絡(luò)訪問的權(quán)限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.net"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NetActivity"
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="7" />
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
6. 這里把strings.xml文件也貼出來(lái)
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, NetActivity!</string> <string name="app_name">圖片查看</string> <string name="picaddress">圖片地址</string> <string name="look">查看</string> <string name="error">網(wǎng)絡(luò)連接異常</string> </resources>
下面是運(yùn)行效果圖:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android圖片壓縮的3種方法實(shí)例
- Android Activity之間傳遞圖片(Bitmap)的方法
- android異步加載圖片并緩存到本地實(shí)現(xiàn)方法
- android保存Bitmap圖片到指定文件夾示例
- Android中Glide加載庫(kù)的圖片緩存配置究極指南
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android從服務(wù)器獲取圖片的實(shí)例方法
- Android讀取assets目錄下的所有圖片并顯示的方法
- Android開發(fā)ImageView圖片無(wú)法顯示解決過(guò)程
- Android截屏保存png圖片的實(shí)例代碼
- Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
相關(guān)文章
Android學(xué)習(xí)筆記之Shared Preference
在之前遇到有個(gè)需求是要改settings里面自動(dòng)轉(zhuǎn)屏的首選項(xiàng),于是就學(xué)習(xí)了下Shared Preference。Shared Preference是一種簡(jiǎn)單的、輕量級(jí)的鍵/值對(duì)機(jī)制,用于保存原始應(yīng)用程序數(shù)據(jù),最常見的就是首選項(xiàng)2013-09-09
Android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
在Android模擬器上模擬GPS功能總是null的解決方法
在我們開發(fā)時(shí)需要在模擬器上模擬GPS,可在Location的時(shí)候總是null,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下哈2013-06-06
Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8)
這篇文章主要介紹了Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Jenkins打包android應(yīng)用時(shí)自動(dòng)簽名apk詳解
這篇文章主要介紹了Jenkins打包android應(yīng)用時(shí)自動(dòng)簽名apk詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Flutter簡(jiǎn)潔實(shí)用的圖片編輯器的實(shí)現(xiàn)
本文主要介紹了Flutter簡(jiǎn)潔實(shí)用的圖片編輯器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android 代碼一鍵實(shí)現(xiàn)銀行卡綁定功能
這篇文章主要介紹了Android 代碼一鍵實(shí)現(xiàn)銀行卡綁定功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

