AsyncTaskダウンロード画像非同期更新UI

10136 ワード

Android UIスレッドでは、ファイルのダウンロード、ネットワーク接続など、時間のかかる操作ができないためです.新しいスレッドで操作する必要があります.多くの場合、取得されたデータは現在のActivityに表示される必要があり、他のスレッドではUIを更新することはできません.一般的には2つの方法があります.1.Handler 2.AsyncTask
前にHandlerの方法を試してみましたが、分かりやすくて煩わしいと感じました.その後、AsyncTaskを紹介する様々な文章を見て、googleのAPI guidesを参照して試してみましたが、いろいろな感じがしました.
次に、AsyncTaskを使用して画像をダウンロードし、現在のUIに更新する例を示します.
この仕事を完成するには2歩しかかからない.
1.クラス継承AsyncTaskを書く
2.OnClickでこのクラスを使う
まずはactivity_を見てmain.xmlファイルですね.簡単なレイアウト、ボタン、ImageViewボックス
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    <Button

        android:id="@+id/button"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/button_string"

        android:layout_below="@id/label"/>

    <ImageView

        android:id="@+id/image"

        android:layout_width="100dp"

        android:layout_height="100dp"

        android:layout_below="@id/button"/>

</RelativeLayout>

次はMainActivity.JAvaというファイル
package com.kross.testasynctask;



import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;



import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;



public class MainActivity extends Activity {
   // Button ImageView
private Button button = null; private ImageView imageView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
     // Button ImageView button
= (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.image);
   // button.setOnClickListener(
new ButtonListener()); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } class ButtonListener implements OnClickListener { @Override public void onClick(View v) {
       // , AsyncTask
new GetImageTask().execute("http://content.52pk.com/files/121122/1284656_175821_1_lit.jpg"); } }
   // , AsyncTask <String, Void, Bitmap> , , 。
   // : , String
   // : , ,
   // : , Bitmap
class GetImageTask extends AsyncTask<String, Void, Bitmap> {      // , execute() protected Bitmap doInBackground(String... urls) { Bitmap bmp = null; try { bmp = this.loadImageFromNetwork(urls[0]); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bmp; } @Override
     // , , Bitmap ImageView
protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); imageView.setImageBitmap(result); }      // private Bitmap loadImageFromNetwork(String imageUrl) throws IOException { URL url = new URL(imageUrl); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setDoInput(true); con.connect(); InputStream inputStream = con.getInputStream(); Bitmap bmp = BitmapFactory.decodeStream(inputStream); return bmp; } } }

参考資料:http://developer.android.com/reference/android/os/AsyncTask.html