最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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實現(xiàn)播放音效

    Android使用SoundPool實現(xiàn)播放音效

    這篇文章主要為大家詳細介紹了Android使用SoundPool實現(xiàn)播放音效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • android webp編解碼詳解

    android webp編解碼詳解

    本文主要講解android webp編解碼,在Android開發(fā)過程中會遇到圖片的上傳和下載,這提供WebP編解碼縮小圖片,便與大家提高用戶體驗,有興趣的小伙伴可以參考下
    2016-08-08
  • android解析JSON數(shù)據(jù)

    android解析JSON數(shù)據(jù)

    本文給大家介紹的是在Android中解析json數(shù)據(jù)的方法的幾種方法,非常的簡單實用,有需要的小伙伴可以參考下
    2016-03-03
  • 淺談Android 照相機權限的聲明

    淺談Android 照相機權限的聲明

    今天小編就為大家分享一篇淺談Android 照相機權限的聲明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 簡單說說Android中如何使用攝像頭和相冊

    簡單說說Android中如何使用攝像頭和相冊

    本篇文章主要介紹了簡單說說Android中如何使用攝像頭和相冊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android7.0以上Uri轉路徑的方法實現(xiàn)(已驗證)

    Android7.0以上Uri轉路徑的方法實現(xiàn)(已驗證)

    這篇文章主要介紹了Android7.0以上Uri轉路徑的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Android使用View Animation實現(xiàn)動畫加載界面

    Android使用View Animation實現(xiàn)動畫加載界面

    這篇文章主要為大家詳細介紹了Android使用View Animation實現(xiàn)動畫加載界面的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android給app設置自定義鈴聲功能

    Android給app設置自定義鈴聲功能

    這篇文章主要為大家詳細介紹了Android給app設置自定義鈴聲功能的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 淺談Volley加載不出圖片的問題

    淺談Volley加載不出圖片的問題

    下面小編就為大家?guī)硪黄獪\談Volley加載不出圖片的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 小談Kotlin的空處理的使用

    小談Kotlin的空處理的使用

    這篇文章主要介紹了小談Kotlin的空處理的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01

最新評論

美姑县| 潞城市| 苍南县| 芜湖市| 孟村| 浪卡子县| 斗六市| 仁化县| 安顺市| 鲜城| 连南| 四川省| 崇仁县| 和林格尔县| 镇平县| 安福县| 洮南市| 四会市| 嘉善县| 临泽县| 固阳县| 双辽市| 山阴县| 徐闻县| 麟游县| 尼玛县| 古田县| 且末县| 长乐市| 涪陵区| 永春县| 清镇市| 焉耆| 南靖县| 通江县| 从江县| 观塘区| 盐源县| 师宗县| 禄丰县| 宕昌县|