package com.ccl.getp_w_picpath; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_path; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void getImage(View view){ String path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "地址不能為空", 0).show(); return; } try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); System.out.println("響應(yīng)碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務(wù)器返回的流數(shù)據(jù) InputStream in = conn.getInputStream(); //將返回的流數(shù)據(jù)解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //顯示圖片 iv.setImageBitmap(bitmap); in.close(); } } catch (Exception e) { e.printStackTrace(); } } }
Android模擬器版本4.1.2
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比北海網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式北海網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋北海地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
異常解釋:在主線程中的網(wǎng)絡(luò)異常
原因:Android2.3版本后不允許在主線程中直接請(qǐng)求網(wǎng)絡(luò)獲取數(shù)據(jù)
解決方法(兩種):
一:onCreate方法中加入如下代碼(不推薦使用,有可能阻塞Android主線程)
if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }
二:使用Android 的handler機(jī)制,另外開(kāi)啟一個(gè)子線程請(qǐng)求網(wǎng)絡(luò)獲取數(shù)據(jù)(推薦使用)
package com.ccl.getp_w_picpath; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_path; private ImageView iv; private String path; //使用Handler更新主線程(UI線程) private Handler handler = new Handler(){ public void handleMessage(Message msg) { Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void getImage(View view){ path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "地址不能為空", 0).show(); return; } new Thread(){ public void run(){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); System.out.println("響應(yīng)碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務(wù)器返回的流數(shù)據(jù) InputStream in = conn.getInputStream(); //將返回的流數(shù)據(jù)解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //使用handler傳遞消息 Message msg = Message.obtain(); msg.obj = bitmap;//傳遞的數(shù)據(jù) handler.sendMessage(msg); in.close(); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
注意兩點(diǎn):
Message對(duì)象的創(chuàng)建使用obtain方法可以達(dá)到對(duì)象重用的目的,節(jié)省內(nèi)存開(kāi)銷。
Message對(duì)象可以使用msg.what = 傳遞消息的類型,handler可以根據(jù)傳遞的消息類型做不同處理,優(yōu)化代碼如下:
package com.ccl.getp_w_picpath; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { protected static final int SUCCESS = 0; protected static final int ERROR = 1; protected static final int NETWORK_ERROR = 2; private EditText et_path; private ImageView iv; private String path; //使用Handler更新主線程(UI線程) private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case SUCCESS: Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); break; case ERROR: Toast.makeText(MainActivity.this, "獲取圖片失敗", 0).show(); break; case NETWORK_ERROR: Toast.makeText(MainActivity.this, "連接網(wǎng)絡(luò)失敗", 0).show(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void getImage(View view){ path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "地址不能為空", 0).show(); return; } new Thread(){ public void run(){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); System.out.println("響應(yīng)碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務(wù)器返回的流數(shù)據(jù) InputStream in = conn.getInputStream(); //將返回的流數(shù)據(jù)解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //使用handler傳遞消息 Message msg = Message.obtain(); msg.obj = bitmap;//傳遞的數(shù)據(jù) msg.what = SUCCESS;//傳遞的消息類型,handler可根據(jù)消息類型做不同處理 handler.sendMessage(msg); in.close(); }else{ Message msg = Message.obtain(); msg.what = ERROR; handler.sendMessage(msg); } } catch (Exception e) { Message msg = Message.obtain(); msg.what = NETWORK_ERROR; handler.sendMessage(msg); e.printStackTrace(); } } }.start(); } }