您的位置:首页 > 其它

实现输入图片地址浏览图片功能

2015-06-30 10:26 399 查看
1.UI设计;



2..实现功能的MainActivity类:

代码如下:

public class MainActivity extends Activity {

 private EditText etUrl;

 private ImageView ivImageShow;

 public static final int SHOWIMAGE=1;

 private Handler handler=new Handler(){

  public void handleMessage(Message msg){

   switch (msg.what) {   

   case SHOWIMAGE:

    Bitmap bitmap=(Bitmap) msg.obj;

    ivImageShow.setImageBitmap(bitmap);

    break;

   default:

    break;

   }

  };

 };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initViews();

    }

    private void initViews(){

     etUrl=(EditText)findViewById(R.id.etImageUrl);

     ivImageShow=(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 showImage(View view){

     final String path=etUrl.getText().toString();

     if(TextUtils.isEmpty(path)){

      Toast.makeText(this, "图片路径不能空", Toast.LENGTH_LONG).show();      

     }else{

      new Thread(){

       public void run(){

        try{

            URL url=new URL(path);

            HttpURLConnection connection=(HttpURLConnection) url.openConnection();

               connection.setRequestMethod("GET");

               connection.setConnectTimeout(5000);

               int responseCode=connection.getResponseCode();

               if(responseCode==200){

                InputStream inputStream=connection.getInputStream();

                   Bitmap bitmap=BitmapFactory.decodeStream(inputStream);

                

                   // ivImageShow.setImageBitmap(bitmap);

                   Message message=new Message();

                   message.what=SHOWIMAGE;

                   message.obj=bitmap;

                   handler.sendMessage(message);

               }

           }catch(MalformedURLException e){

            e.printStackTrace();

           } catch (IOException e) {

         // TODO Auto-generated catch block

         e.printStackTrace();

        }

       };

      }.start();

     }

    }

}

3.布局 activity_main.xml 代码如下:

   <EditText

        android:id="@+id/etImageUrl"

        android:layout_width="match_parent"

        android:layout_height="108dp"

        android:ems="10"       

        android:text="http://172.22.64.20:8080/test/images/0.jpg" />

        <requestFocus />

    </EditText>

    <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="showImage"

        android:text="浏览" />

   

    <ImageView

        android:id="@+id/ivImage"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_weight="1" />

   

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片