您好,登錄后才能下訂單哦!
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("響應碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務器返回的流數據 InputStream in = conn.getInputStream(); //將返回的流數據解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //顯示圖片 iv.setImageBitmap(bitmap); in.close(); } } catch (Exception e) { e.printStackTrace(); } } }
Android模擬器版本4.1.2
異常解釋:在主線程中的網絡異常
原因:Android2.3版本后不允許在主線程中直接請求網絡獲取數據
解決方法(兩種):
一:onCreate方法中加入如下代碼(不推薦使用,有可能阻塞Android主線程)
if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }
二:使用Android 的handler機制,另外開啟一個子線程請求網絡獲取數據(推薦使用)
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("響應碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務器返回的流數據 InputStream in = conn.getInputStream(); //將返回的流數據解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //使用handler傳遞消息 Message msg = Message.obtain(); msg.obj = bitmap;//傳遞的數據 handler.sendMessage(msg); in.close(); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
注意兩點:
Message對象的創建使用obtain方法可以達到對象重用的目的,節省內存開銷。
Message對象可以使用msg.what = 傳遞消息的類型,handler可以根據傳遞的消息類型做不同處理,優化代碼如下:
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, "連接網絡失敗", 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("響應碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務器返回的流數據 InputStream in = conn.getInputStream(); //將返回的流數據解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //使用handler傳遞消息 Message msg = Message.obtain(); msg.obj = bitmap;//傳遞的數據 msg.what = SUCCESS;//傳遞的消息類型,handler可根據消息類型做不同處理 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(); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。