Android Loading pictures

Source: Internet
Author: User

 PackageMydemo.mycom.demo2;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classImageprevextendsActionbaractivityImplementsView.onclicklistener {PrivateImageView IV; PrivateButton Btn_prev_image; PrivateEditText Et_image_path; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_image_prev); IV=(ImageView) Findviewbyid (R.ID.IV); Btn_prev_image=(Button) Findviewbyid (r.id.btn_prev_image); Et_image_path=(EditText) Findviewbyid (R.id.et_image_path); Btn_prev_image.setonclicklistener ( This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.menu_image_prev, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); } @Override Public voidOnClick (view view) {Switch(View.getid ()) { Caser.id.btn_prev_image:string Path=Et_image_path.gettext (). toString (). Trim (); if(Textutils.isempty (path)) {Toast.maketext ( This, "Picture path cannot", Toast.length_short). Show (); return; }                Try                {                    //Browse PicturesURL url =NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); intCode =Conn.getresponsecode (); if(code==200) {InputStream is=Conn.getinputstream (); Bitmap Bitmap=Bitmapfactory.decodestream (IS);                    Iv.setimagebitmap (bitmap); }                    Else{Toast.maketext ( This, "Picture Browse failed", Toast.length_short). Show (); }                }Catch(Exception e) {Toast.maketext ( This, "Picture Browse failed", Toast.length_short). Show ();                E.printstacktrace (); }                 Break; }    }}

Activity

<LinearLayoutxmlns: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:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"android:orientation= "vertical"Tools:context= "Mydemo.mycom.demo2.ImagePrev">    <ImageViewAndroid:layout_weight= "+"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:id= "@+id/iv"/>    <EditTextAndroid:text= "Http://192.168.1.1:91/Resource/TopicInfo/2015-05-23/130768288778069472.png"Android:hint= "Picture path"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/et_image_path"/>    <ButtonAndroid:text= "Browse"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/btn_prev_image"/></LinearLayout>

Android Loading pictures

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.