步骤:
1.页面
2.获取url
3.获取http对象的连接
4.获取图片Bitmap
5.显示页面
6.权限
注意:如果在子线程中显示图片的话
CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
只有原始的线程(主线程, ui线程)才能修改view对象.
在子线程中修改view的显示状态, 会报上面异常.
<span style="font-size:14px;"><LinearLayout 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"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="match_parent"
android:layout_height="dip"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_url"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg"
android:layout_weight="1"
android:singleLine="true"/>
<Button
android:id="@+id/btn_subimt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Go"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
</span>
package com.sqlnetphoto;
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.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText etUrl;
private ImageView ivIcon;
private static final int SUCCESS = 0;
private static final int ERROR = 1;
private Handler handler = new Handler(){//import android.os.Handler;
/**
* 接收消息 主线程回调用此方法
* */
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Log.i(TAG, "what = " +msg.what);
if(msg.what == SUCCESS){ //当前是访问网络,取显示图片
Bitmap bitmap = (Bitmap) msg.obj;
ivIcon.setImageBitmap(bitmap); //设置演示图片
}else if(msg.what == ERROR){
Toast.makeText(MainActivity.this, "抓取失败", 0).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivIcon = (ImageView) findViewById(R.id.iv_icon);
etUrl = (EditText) findViewById(R.id.et_url);
findViewById(R.id.btn_subimt).setOnClickListener(this);
}
@Override
public void onClick(View v) {
final String url = etUrl.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = getImageFromNet(url); //防止服务器异常阻塞
//ivIcon.setImageBitmap(bitmap); //设置演示图片 这里回报异常
if(bitmap != null){
Message msg = new Message(); //消息处理对象
msg.what = SUCCESS;
msg.obj = bitmap; //把图片信息带给主线程
handler.sendMessage(msg);
}else{
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
}
}).start();
}
/**
* 根据url连接获取网络抓去图片返回
*
* Bitmap 相对于 图片
* */
private Bitmap getImageFromNet(String url){
HttpURLConnection conn = null;
try {
URL mURL = new URL(url); //创建一个url
//得到http的连接对象
conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod("GET"); //设置请求方式为get
conn.setConnectTimeout(10*1000); //设置连接服务器的超时时间,如果超出设置时间,没有连接成功,回抛异常
conn.setReadTimeout(5*1000); //设置读取数据超出时间,如果超出5s,抛异常
conn.connect();// 开始连接
int responseCode = conn.getResponseCode(); //得到服务器响应
if(responseCode == 200){ //访问成功
InputStream is = conn.getInputStream(); //获得服务器返回的流数据
Bitmap bitmap = BitmapFactory.decodeStream(is); //根据 流数据 创建一个bitmap位图对象
return bitmap;
}else {
Log.i(TAG, "访问失败: responseCode = " +responseCode);
}
} catch ( Exception e) {
e.printStackTrace();
}finally{
if(conn != null){
conn.disconnect(); //断开连接
}
}
return null;
}
}
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sqlnetphoto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<!-- inter权限 -->
<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=".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>
</span>