欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android查看网络图片的实现方法

程序员文章站 2023-11-09 13:15:10
本文实例为大家分享了android查看网络图片的具体代码,供大家参考,具体内容如下 需求描述: 输入一个 图片地址,下载到本地 展示。 效果展示 代码清单 ma...

本文实例为大家分享了android查看网络图片的具体代码,供大家参考,具体内容如下

需求描述: 输入一个 图片地址,下载到本地 展示。

效果展示

android查看网络图片的实现方法

代码清单

mainactivity.java

package com.example.www.checkimage;

import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.bundle;
import android.os.handler;
import android.os.message;
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.inputstream;
import java.net.httpurlconnection;
import java.net.url;

public class mainactivity extends appcompatactivity {

 private edittext mpt_url;
 private imageview miv_show;
 private handler mhandler = new handler(){
 @override
 public void handlemessage(message msg) {
 if(msg.what == 1){
 bitmap bitmap = (bitmap) msg.obj;
 miv_show.setimagebitmap(bitmap);
 toast.maketext(getapplicationcontext(), "图片展示成功", toast.length_long).show();
 }
 }
 };

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);

 mpt_url = (edittext) findviewbyid(r.id.pt_url);
 miv_show = (imageview) findviewbyid(r.id.imagecon);
 }

 public void checkimage(view v) {
 new thread(){
 @override
 public void run() {

 try {
  string path = mpt_url.gettext().tostring().trim();
  url url = new url(path);
  httpurlconnection conn = (httpurlconnection) url.openconnection();
  conn.setrequestmethod("get");
  conn.setconnecttimeout(5000);
  int responsecode = conn.getresponsecode();
  if(responsecode == 200) {
  inputstream is = conn.getinputstream();


  bitmap bitmap = bitmapfactory.decodestream(is);

  message msg = message.obtain(); // 创建 消息
  msg.obj = bitmap;
  msg.what = 1;


  mhandler.sendmessage(msg);
  }
 } catch (exception e) {
  e.printstacktrace();
 }

 }
 }.start();
 }
}

activiity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".mainactivity">

 <edittext
 android:id="@+id/pt_url"
 android:layout_width="368dp"
 android:layout_height="wrap_content"
 android:layout_marginstart="8dp"
 android:layout_margintop="8dp"
 android:layout_marginend="8dp"
 android:ems="10"
 android:hint="请输入与图片地址"
 android:inputtype="textpersonname"
 app:layout_constraintend_toendof="parent"
 app:layout_constraintstart_tostartof="parent"
 app:layout_constrainttop_totopof="parent" />

 <button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginstart="8dp"
 android:layout_margintop="8dp"
 android:layout_marginend="8dp"
 android:text="button"
 app:layout_constraintend_toendof="parent"
 app:layout_constrainthorizontal_bias="0.0"
 app:layout_constraintstart_tostartof="parent"
 android:onclick="checkimage"
 app:layout_constrainttop_tobottomof="@+id/pt_url" />

 <imageview
 android:id="@+id/imagecon"
 android:layout_width="0dp"
 android:layout_height="0dp"
 android:layout_marginstart="8dp"
 android:layout_margintop="8dp"
 android:layout_marginend="8dp"
 android:layout_marginbottom="8dp"
 app:layout_constraintbottom_tobottomof="parent"
 app:layout_constraintend_toendof="parent"
 app:layout_constraintstart_tostartof="parent"
 app:layout_constrainttop_tobottomof="@+id/button"
 app:srccompat="@mipmap/ic_launcher" />
</android.support.constraint.constraintlayout>

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.www.checkimage">

 <uses-permission android:name="android.permission.internet"/>

 <application
 android:allowbackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:roundicon="@mipmap/ic_launcher_round"
 android:supportsrtl="true"
 android:theme="@style/apptheme">
 <activity android:name=".mainactivity">
 <intent-filter>
 <action android:name="android.intent.action.main" />

 <category android:name="android.intent.category.launcher" />
 </intent-filter>
 </activity>
 </application>

</manifest>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。