关于安卓图片搜索

1.创建界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.example.bz0209.xzx.MainActivity">


    <ImageButton


        android:id="@+id/main_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <EditText


        android:id="@+id/main_text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/main_text3"
        android:layout_toEndOf="@+id/main_text1"
        android:layout_toRightOf="@+id/main_text1"
        android:hint="请输入图片路径" />


    <Button
        android:id="@+id/main_text3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:hint="浏览"
        android:onClick="click" />




</LinearLayout>

2.编写package com.example.bz0209.xzx;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import Java.io.IOException;
import java.io.InputStream;
import java.NET.HttpURLConnection;
import java.Net.MalformedURLException;
import java.net.URL;




public class MainActivity extends AppCompatActivity {
    private  static final int GETM=1;
    private EditText main_text2;
    private ImageButton main_text1;
    private Button main_text3;
    private int SHOW_IMAGE;
    private Handler handler=new Handler(){




        public void handleMessage(Message msg){
            switch (msg.what){
                case GETM:
                    Bitmap bitmap=(Bitmap) msg.obj;
                    main_text1.setImageBitmap(bitmap);
                    break;
                default:
                    break;
            }
        };
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_text2=(EditText)findViewById(R.id.main_text2);
        main_text1=(ImageButton)findViewById(R.id.main_text1);
    }


public void click(View view) {
    final String path = main_text2.getText().toString();
    if (TextUtils.isEmpty(path)) {
        Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show();
    } else {
        new Thread() {
            public void run() {
                try {
                    URL ur1 = new URL(path);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) ur1.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setConnectTimeout(5000);
                    int responsecode = httpURLConnection.getResponseCode();
                    if (responsecode == 200) {
                        InputStream is = httpURLConnection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
//                        main_text1.setImageBitmap(bitmap);
                        Message message = new Message();
                        message.what=GETM;
                        message.obj=bitmap;
                        handler.sendMessage(message);
                    } else {
                        Toast.makeText(MainActivity.this, "显示图片失败", Toast.LENGTH_SHORT).show();


                    }






                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            };




        }.start();
    }


}








}mainactivity

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值