Andriod 讀取網(wǎng)絡圖片實例代碼解析
Android手機上,我們常用ImageView顯示圖片,我們本章獲取網(wǎng)絡圖片并顯示在ImageView中。
一、設計界面
1、布局文件
打開res/layout/activity_main.xml文件。
輸入以下代碼:
<?xml version="." encoding="utf-"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imagephoto" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
二、程序文件
打開“src/com.genwoxue.networkphoto/MainActivity.java”文件。
然后輸入以下代碼:
package com.genwoxue.networkphoto;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imView = (ImageView) findViewById(R.id.imagephoto);
String imageUrl = "http://img.baidu.com/img/image/ilogob.gif";
new NetworkPhoto().execute(imageUrl);
}
/* 四個步驟:
* ()onPreExecute(),執(zhí)行預處理,它運行于UI線程,
* 可以為后臺任務做一些準備工作,比如繪制一個進度條控件。
* ()doInBackground(Params...),后臺進程執(zhí)行的具體計算在這里實現(xiàn),
* doInBackground(Params...)是AsyncTask的關(guān)鍵,此方法必須重載。
* 在這個方法內(nèi)可以使用 publishProgress(Progress...)改變當前的進度值。
* ()onProgressUpdate(Progress...),運行于UI線程。如果
* 在doInBackground(Params...) 中使用了publishProgress(Progress...),就會
* 觸發(fā)這個方法。在這里可以對進度條控件根據(jù)進度值做出具體的響應。
* ()onPostExecute(Result),運行于UI線程,可以對后臺任務的結(jié)果做出處理,結(jié)果
* 就是doInBackground(Params...)的返回值。此方法也要經(jīng)常重載,如果Result為
* null表明后臺任務沒有完成(被取消或者出現(xiàn)異常)。 *
*/
//本案例我們僅使用了()和()
class NetworkPhoto extends AsyncTask<String, Integer, Bitmap> {
public NetworkPhoto() {
}
//doInBackground(Params...),后臺進程執(zhí)行的具體計算在這里實現(xiàn),是AsyncTask的關(guān)鍵,此方法必須重載。
@Override
protected Bitmap doInBackground(String... urls) {
URL url = null;
Bitmap bitmap = null;
HttpURLConnection conn=null;
InputStream is=null;
try {
url = new URL(urls[]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(conn!=null){
conn.disconnect();
conn=null;
}
if(is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is=null;
}
}
return bitmap;
}
//onPostExecute(Result),運行于UI線程,可以對后臺任務的結(jié)果做出處理,結(jié)果
//就是doInBackground(Params...)的返回值。
@Override
protected void onPostExecute(Bitmap bitmap) {
// 返回結(jié)果bitmap顯示在ImageView控件
imView.setImageBitmap(bitmap);
}
}
}
三、配置文件
打開“AndroidManifest.xml”文件。
然后輸入以下代碼:
<?xml version="." encoding="utf-"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.genwoxue.networkphoto" android:versionCode="" android:versionName="." > <uses-sdk android:minSdkVersion="" android:targetSdkVersion="" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.genwoxue.networkphoto.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> </manifest>
注意:需要在AndroidManifest.xml文件中添加權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
相關(guān)文章
當Flutter遇到節(jié)流與防抖的思路和流程優(yōu)化
這篇文章主要給大家介紹了關(guān)于當Flutter遇到節(jié)流與防抖的思路和流程優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01
詳解Android中Fragment的兩種創(chuàng)建方式
本篇文章主要介紹了Android中Fragment的兩種創(chuàng)建方式,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
Android開發(fā)中的文件操作工具類FileUtil完整實例
這篇文章主要介紹了Android開發(fā)中的文件操作工具類FileUtil,結(jié)合完整實例形式分析了Android文件操作的常用技巧,包括文件的獲取、遍歷、搜索、復制、刪除、判斷等功能,需要的朋友可以參考下2017-11-11
Android巧用Fragment解耦onActivityResult詳解
這篇文章主要給大家介紹了關(guān)于Android巧用Fragment解耦onActivityResult的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08

