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>
这是一个完整的实例,可直接运行于模拟器或真机。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值