Android使用线程获取网络图片的方法
作者:lijiao 时间:2023-05-28 22:29:28
本文为大家分享了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);
}
}
}
标签:Android,线程,网络图片
0
投稿
猜你喜欢
Android使用AsyncQueryHandler实现获取手机联系人功能
2021-07-30 20:12:00
C# XML基础入门小结(XML文件内容增删改查清)
2022-10-18 17:12:02
C#的3DES加密解密算法实例代码
2023-09-23 01:10:19
Android亮屏速度分析总结
2023-11-06 11:55:21
Java获取视频时长及截取帧截图详解
2022-03-28 20:52:53
Android开发之搜索框SearchView用法示例
2021-10-30 03:40:19
详解Java中Duration类的使用方法
2021-07-30 20:09:28
基于C#实现磁性吸附窗体
2022-09-04 07:04:51
Android仿360悬浮小球自定义view实现示例
2021-10-22 10:16:14
Java Swing JProgressBar进度条的实现示例
2023-07-15 17:48:47
Android 启动activity的4种方式及打开其他应用的activity的坑
2023-12-25 01:18:48
PC蓝牙通信C#代码实现
2023-07-06 19:59:32
SpringBoot结合SpringSecurity实现图形验证码功能
2023-02-25 16:04:52
Android不规则封闭区域填充色彩的实例代码
2022-10-17 08:33:05
android studio实现简单的计算器小功能
2022-07-22 17:53:26
从零开始Java实现Parser Combinator
2023-06-18 18:52:04
Java乱码问题解决方法_动力节点Java学院整理
2021-07-25 01:04:52
详解C#实现在Excel单元格中应用多种字体格式
2023-01-25 07:58:52
Android ScrollView滑动实现仿QQ空间标题栏渐变
2021-06-13 16:32:19
详解Android 中的文件存储
2023-12-21 17:44:44