android网络图片查看器简单实现代码
程序员文章站
2023-12-12 11:50:22
本文实例为大家分享了android网络图片查看器的具体代码,供大家参考,具体内容如下
效果图:
1.输入一个图片url
2.转换成bitmap位图
3.展示到i...
本文实例为大家分享了android网络图片查看器的具体代码,供大家参考,具体内容如下
效果图:
1.输入一个图片url
2.转换成bitmap位图
3.展示到imageview上
xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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: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.itheima74.internetpicturelook.mainactivity"> <edittext android:id="@+id/et_url" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparenttop="true" android:hint="请输入图片网址" android:inputtype="texturi" android:text="http://b.hiphotos.baidu.com/image/pic/item/d009b3de9c82d15825ffd75c840a19d8bd3e42da.jpg" /> <button android:id="@+id/bt_look" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_url" android:text="查看图片" /> <scrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/bt_look" android:layout_centerhorizontal="true"> <imageview android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </scrollview> </relativelayout>
java代码:
package com.itheima74.internetpicturelook; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.edittext; import android.widget.imageview; import android.widget.toast; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; public class mainactivity extends appcompatactivity { private edittext et_url; private imageview iv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); et_url = (edittext) findviewbyid(r.id.et_url); iv = (imageview) findviewbyid(r.id.iv); findviewbyid(r.id.bt_look).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string path = et_url.gettext().tostring().trim(); if (path.isempty()) { toast.maketext(mainactivity.this, "请输入图片网址", toast.length_short).show(); } else { //开启子线程去网络下载图片 downloadpicture(path); } } private void downloadpicture(final string path) { // 子线程请求网络 new thread() { @override 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(); final bitmap bitmap = bitmapfactory.decodestream(inputstream); // 主线程更新ui runonuithread(new runnable() { @override public void run() { iv.setimagebitmap(bitmap); } }); } } catch (ioexception e) { e.printstacktrace(); } } }.start(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。