Android Network Image Viewer,
Today, we will implement a simple network image viewer in android.
The interface is as follows:
The Code is as follows:
<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" tools: context = ". mainActivity "> <ImageView android: id =" @ + id/iv "android: layout_width =" fill_parent "android: layout_height =" fill_parent "android: layout_weight = "1000"/> <EditText android: singleLine = "true" android: text = "http://img01.sogoucdn.com/app/a/100540002/1047586.jpg" android: id = "@ + id/et_path" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "Enter the image path"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "click" android: text = ""/> </LinearLayout>
The Code is as follows:
Package com. wuyudong. imagesviewer; import java. io. inputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import org. apache. http. httpConnection; import android. OS. bundle; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. text. textUtils; import android. view. menu; 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 click (View view) {String path = et_path.getText (). toString (). trim (); if (TextUtils. isEmpty (path) {Toast. makeText (this, "image path cannot be blank", 0 ). show () ;}else {// connect to the Server get request to obtain the image try {URL url = new URL (path); // send the http request HttpURLConnection conn = (HttpURLConnection) based on the url) url. openConnection (); // set the Request Method conn. setRequestMethod ("GET"); conn. setConnectTimeout (5000); conn. setreadtimeouts (5000); conn. setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 "); int code = conn. getResponseCode (); if (code = 200) {InputStream is = conn. getInputStream (); Bitmap bitmap = BitmapFactory. decodeStream (is); iv. setImageBitmap (bitmap);} else {Toast. makeText (this, "failed to display image", 0 ). show () ;}} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace (); Toast. makeText (this, "failed to access the image", 0 ). show ();}}}}
Add permission: android. permission. INTERNET
Error: android. OS. NetworkOnMainThreadException
To explain, google no longer allows network requests (HTTP, Socket) and other related operations directly in the Main Thread class starting with Honeycomb SDK (3.0). In fact, this should not be done in this way, network operations directly on the UI thread will block the UI and the user experience is quite poor!
Therefore, in versions earlier than Honeycomb SDK (3.0), you can continue to do so in Main Thread. In Versions later than 3.0, it won't work.
For specific solutions, refer to the following document.