日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android使用ImageView显示网络图片

發布時間:2025/3/18 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android使用ImageView显示网络图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本案例使用ImageView 簡單的實現了網絡圖片的調用。當中注意事項。由于用到了網絡,這里採用了HttpClient方法訪問網絡聯接,關于怎樣使用,可參照文章?Android中使用HttpClient實現HTTP通信效果?,因此。須要注意配置網絡權限問題。以及須要使用新線程及Handler來更新Activity,不然會直接報錯Not Main Thread 看實例: MainActivity.java package com.example.imageview;import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.graphics.Bitmap; import android.view.Menu; import android.widget.ImageView;public class MainActivity extends Activity { private Bitmap bm = null;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);final Handler handler = new Handler(); new Thread() { public void run() { bm = new ApacheHttpClient() .getHttpBmp("http://www.qilujiaju.com/data/attachment/block/c9/c960ba426890a8ddbfc35d2b4b0d97c9.jpg"); handler.post(new Runnable() {@Override public void run() { // TODO Auto-generated method stub ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap(bm); } }); } }.start(); }@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; }} ApacheHttpClient.java package com.example.imageview;import java.io.IOException; import java.io.InputStream;import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient;import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log;public class ApacheHttpClient { private static final String TAG = "Error";public InputStream httpGet(String url) { InputStream result = null; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); int httpStatus = httpResponse.getStatusLine().getStatusCode(); if (httpStatus == HttpStatus.SC_OK) { InputStream in = httpResponse.getEntity().getContent(); try { result = in; } catch (Exception e) { Log.i(TAG, "Exception"); // TODO Auto-generated catch block e.printStackTrace(); } } else { result = null; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i(TAG, "ClientProtocolException"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i(TAG, "ClientProtocolException"); } return result; }public Bitmap getHttpBmp(String url) { Bitmap bm = null; InputStream is = httpGet(url); bm = BitmapFactory.decodeStream(is); return bm; } } AndroidMainFest.xml <?

xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.imageview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <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.example.imageview.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>

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" ><ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/app" /></RelativeLayout> 這是一個完整的實例,可直接執行于模擬器或真機。

總結

以上是生活随笔為你收集整理的Android使用ImageView显示网络图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。