Android Online View images

Source: Internet
Author: User

Mainactivity.java

Package Com.example.internetimageview;
Import java.io.IOException;
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.os.Bundle;
Import Android.os.Handler;
Import Android.os.Looper;
Import Android.os.Message;
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 Etimageurl;
Private ImageView ivimage;
public static final int showimage=1;
Private Handler handler=new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case SHOWIMAGE:
Bitmap bitmap= (Bitmap) msg.obj;
Ivimage.setimagebitmap (bitmap);
Break
Default
Break
}
};
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initviews ();
}
private void Initviews () {
Etimageurl= (EditText) Findviewbyid (R.id.etimageurl);
Ivimage= (ImageView) Findviewbyid (r.id.ivimage);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
public void Viewimage (view view) {
Final String Imageurl=etimageurl.gettext (). toString ();
if (Textutils.isempty (IMAGEURL)) {
Toast.maketext (Getapplicationcontext (), "Picture path cannot be empty", Toast.length_long). Show ();
}else{
New Thread () {

public void Run () {
try {
URL url=new url (imageUrl);
HttpURLConnection httpurlconnection= (httpurlconnection) url.openconnection ();
Httpurlconnection.setrequestmethod ("GET");
Httpurlconnection.setconnecttimeout (5000);
int Responsecode=httpurlconnection.getresponsecode ();
if (responsecode==200) {
InputStream Inputstream=httpurlconnection.getinputstream ();
Bitmap Bitmap=bitmapfactory.decodestream (InputStream);
Message message=new message ();
Message.what=showimage;
Message.obj=bitmap;
Ivimage.setimagebitmap (bitmap);
Handler.sendmessage (message);

}else{
Looper.prepare ();
Toast.maketext (Mainactivity.this, "show Picture Failed", Toast.length_long). Show ();
Looper.loop ();
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}.start ();

}
}

}

Activity-main.xml

<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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">
<imageview
Android:id= "@+id/ivimage"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
android:layout_weight= "1"/>
<edittext
Android:id= "@+id/etimageurl"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:ems= "10"
Android:hint= "Please enter the address of the image"
android:text= "Http://www.2cto.com/uploadfile/Collfiles/20141201/2014120109192831.png"/>
<button
Android:id= "@+id/btnview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
android:background= "@drawable/BUTTON_BG"
android:onclick= "Viewimage"
android:text= "Browse"/>
</LinearLayout>

Android Online View images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.