Android使用线程获取网络图片的方法

 更新时间:2016年06月08日 15:25:56   投稿:lijiao  
这篇文章主要为大家详细介绍了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);
    }
  }
}

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。

相关文章

  • Android使用SoundPool实现播放音效

    Android使用SoundPool实现播放音效

    这篇文章主要为大家详细介绍了Android使用SoundPool实现播放音效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • android webp编解码详解

    android webp编解码详解

    本文主要讲解android webp编解码,在Android开发过程中会遇到图片的上传和下载,这提供WebP编解码缩小图片,便与大家提高用户体验,有兴趣的小伙伴可以参考下
    2016-08-08
  • android解析JSON数据

    android解析JSON数据

    本文给大家介绍的是在Android中解析json数据的方法的几种方法,非常的简单实用,有需要的小伙伴可以参考下
    2016-03-03
  • 浅谈Android 照相机权限的声明

    浅谈Android 照相机权限的声明

    今天小编就为大家分享一篇浅谈Android 照相机权限的声明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 简单说说Android中如何使用摄像头和相册

    简单说说Android中如何使用摄像头和相册

    本篇文章主要介绍了简单说说Android中如何使用摄像头和相册,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android7.0以上Uri转路径的方法实现(已验证)

    Android7.0以上Uri转路径的方法实现(已验证)

    这篇文章主要介绍了Android7.0以上Uri转路径的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Android使用View Animation实现动画加载界面

    Android使用View Animation实现动画加载界面

    这篇文章主要为大家详细介绍了Android使用View Animation实现动画加载界面的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Android给app设置自定义铃声功能

    Android给app设置自定义铃声功能

    这篇文章主要为大家详细介绍了Android给app设置自定义铃声功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • 浅谈Volley加载不出图片的问题

    浅谈Volley加载不出图片的问题

    下面小编就为大家带来一篇浅谈Volley加载不出图片的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 小谈Kotlin的空处理的使用

    小谈Kotlin的空处理的使用

    这篇文章主要介绍了小谈Kotlin的空处理的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01

最新评论