Android使用线程获取网络图片的方法
作者:lijiao 发布时间:2023-05-28 22:29:28
标签:Android,线程,网络图片
本文为大家分享了Android使用线程获取网络图片的具体代码,供大家参考,具体内容如下
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zdcrobot.handlermessage">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.zdcrobot.handlermessage.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载图片"/>
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="500dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.class
package com.zdcrobot.handlermessage;
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.Button;
import android.widget.ImageView;
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 Button button;
private ImageView imageView;
private String imagPath = "http://pica.nipic.com/2007-11-09/200711912453162_2.jpg";
private final int IS_FINISH = 1;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap)msg.obj;
imageView.setImageBitmap(bitmap);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
imageView = (ImageView)findViewById(R.id.image1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new MyClass()).start();
}
});
}
public class MyClass implements Runnable{
@Override
public void run() {
Bitmap bitmap = null;
try {
URL url = new URL(imagPath);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message message = Message.obtain();
message.obj = bitmap;
message.what = IS_FINISH;
handler.sendMessage(message);
}
}
}


猜你喜欢
- 目录1)在程序集中添加资源2)在程序集中查找资源这一篇单独拿出来分析这个程序集资源,为的就是不想让大家把程序集资源和exe程序强关联,因为程
- rocketmq client 日志的问题处理使用rocketmq后,默认会在{user.home}\logs\rocketmqlogs 目
- 源码:class T {int m = 8;}T t = new T();汇编码:0 new #2 <T>3 dup4 invo
- 前言由于大部分android初级教程都没教怎么选择时间,初学者碰到这种难免会有些不知所措,难道要让用户自己输入日期时间?先不说用户体验不好,
- Java(和其他语言)通过内部类支持嵌套类。要使其正常工作,需要编译器执行一些技巧。这是一个例子:public class Outer {
- MyEclipse配置IDEA配置Tomcat环境IDEA:2020.2Tomcat:apache-tomcat-9.0.38创建Web项目
- 1.android中利用webview调用网页上的js代码。Android 中可以通过webview来实现和js的交互,在程序中调用js代码
- 动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而
- 一、连接客户端原理流程图二、功能要求1)连接成功后,可以将服务器发来的消息不停地显示在 listbox 中;2) 客户端要发给服务器的数据,
- Monkeyrunner 常用按键 &nbs
- kafka-console-consumer.sh解读kafka-console-consumer.sh 脚本是一个简易的消费者控制台。该
- 什么是耦合性耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量。模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关系、
- 单链表:每个数据是以节点的形式存在的每个节点分为数据域和指针域数据域中保存该节点的数据指针域中保存指向下一个节点的指针实现思路:节点类Sin
- XML中的扫描过程<?xml version="1.0" encoding="utf-8" ?
- 结构体有时候我们仅需要一个小的数据结构,类提供的功能多于我们需要的功能;考虑到性能原因,最好使用结构体。结构体是值类型,存储在栈中或存储为内
- 思路:先从集合中找出来顶级的菜单,然后遍历顶级菜单,找出每个顶级菜单的所有子菜单,然后判断当前需要排列的集合是否为空,如果不为空的话,就在遍
- 前言之前有人提到过取绝对值时 直接写三目运算符比用Mathf.Abs()效率高 没觉得能高太多今天测了一下 真是不测不知道 一测吓一跳 直接
- Gateway什么是Gateway  由于Netflix的zuul发生问题,spring公司自己研发了一
- 本篇分享的是springboot多数据源配置,在从springboot v1.5版本升级到v2.0.3时,发现之前写的多数据源的方式不可用了
- 一、事件背景个人感觉自己做性能测试,可以说是轻车熟路了,而且工作多年一直都是这一套测试思路及体系,从未质疑过自己,也许是狮子座的迷之自信吧!