Android中实现输入图片地址浏览图片操作

来源:互联网 发布:平安车险计算器软件 编辑:程序博客网 时间:2024/05/22 05:33

其中这两个java文件 请参考 上一篇 文章

 

public class HttpUtils {public static String sendGet(String path) {String content = null;try {// 设置访问urlURL url = new URL(path);// 打开请求HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();// 设置请求的信息httpURLConnection.setRequestMethod("GET");// 设置请求是否超时时间httpURLConnection.setConnectTimeout(5000);// 判断服务器是否响应成功if (httpURLConnection.getResponseCode() == 200) {// 获取响应的输入流对象InputStream is = httpURLConnection.getInputStream();byte data[] = StreamTools.isToData(is);// 把转换成字符串content = new String(data);// 内容编码方式if (content.contains("gb2312")) {content = new String(data, "gb2312");}}// 断开连接httpURLConnection.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return content;}public static Bitmap sendGets(String path) {Bitmap bitmap = null;try {URL url = new URL(path);// 打开请求HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();// 设置请求的信息httpURLConnection.setRequestMethod("GET");// 设置请求是否超时时间httpURLConnection.setConnectTimeout(5000);// 判断服务器是否响应成功if (httpURLConnection.getResponseCode() == 200) {// 获取响应的输入流对象InputStream is = httpURLConnection.getInputStream();System.out.println("-----" + is);// 直接把这个is流转换成Bitmap对象bitmap = BitmapFactory.decodeStream(is);}// 断开连接httpURLConnection.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return bitmap;}}


 

 

实现其功能MainActivity类:

public class MainActivity extends Activity {// 声明控件public EditText et_url;public ImageView iv_ie;// 网络操作类public NetWorkUtils netWorkUtils;private Handler handler;public Bitmap bm;public static final int TEXT = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取et_url对象et_url = (EditText) findViewById(R.id.et_url);iv_ie = (ImageView) findViewById(R.id.iv_ie);// 实例化netWorkUtils = new NetWorkUtils(this);// 实例化这个处理者handler = new Handler() {public void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case TEXT:iv_ie.setImageBitmap(bm);// 设置显示的文本break;default:break;}}};}@Overridepublic 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 sendHttp(View v){//设置网络boolean flag = netWorkUtils.setActiveNetWork();if(flag){//获取路径的值final String path = et_url.getText().toString();if(TextUtils.isEmpty(path)){Toast.makeText(MainActivity.this, "访问的url地址不能为空", Toast.LENGTH_LONG).show();}else{//子 线程new Thread(new Runnable() {@Overridepublic void run() {bm = HttpUtils.sendGets(path);handler.sendEmptyMessage(TEXT);}}).start();}}}}


==========================

布局:

<EditText        android:id="@+id/et_url"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:ems="10"        android:text="@string/url_text" >        <requestFocus />    </EditText>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/et_url"        android:onClick="sendHttp"        android:text="@string/btn_text" />    <ImageView        android:id="@+id/iv_ie"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button1"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:layout_below="@+id/button1"        android:src="@drawable/ic_launcher" />


String文件

<string name="url_text">http://172.22.64.20:8080/test/images/0.jpg</string>
    <string name="btn_text">浏览</string>

 

记得添加访问网络的权限 ,请参考上一篇文章

 

原创粉丝点击