Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [android開發實例]通過Http協議下載圖片簡易版

[android開發實例]通過Http協議下載圖片簡易版

編輯:關於Android編程

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


public class MainActivity extends Activity {
   private Button startDownloadBtn;
   private ImageView resultImageView;
//    private static final String IMAGE_URL = http://e.hiphotos.baidu.com/image/pic/item/e850352ac65c10389f9a68c3b0119313b17e89cf.jpg;
   private static final String IMAGE_URL = http://d.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef9544b321fefaaf51f3de6652.jpg;


    private DownloadThread downloadThread;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        startDownloadBtn = (Button) findViewById(R.id.startDownloadBtn);
        startDownloadBtn.setOnClickListener(new mOnclickListener());
        resultImageView = (ImageView) findViewById(R.id.resultImageView);
        downloadThread = new DownloadThread(IMAGE_URL);

    }

    class mOnclickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //開啟新線程下載
           downloadThread.start();
        }
    }
     class DownloadThread extends Thread{
        private String imageurl;
         private MyHandler myHandler = new MyHandler();
         private Message msg = null;


         public DownloadThread( String url) {
            this.imageurl = url;
        }

        @Override
        public void run() {
            try {
                URL url = new URL(imageurl);
                URLConnection connection = url.openConnection();
                connection.connect();
                InputStream is = connection.getInputStream();
                Log.i(LENGTH, LENGTH----------------- + connection.getContentLength());
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                is.close();
                //輸入流關閉後說明已經獲取了圖像,這時候可以通過handler傳送數據
                msg = new Message();
                msg.obj = bitmap;
                //Handler 不能在另外一個線程任務裡面實例化
                myHandler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    class MyHandler extends  Handler{

        @Override
        public void handleMessage(Message msg) {
            //顯示圖片
            Bitmap bitmap = (Bitmap)msg.obj;
            resultImageView.setImageBitmap(bitmap);
            super.handleMessage(msg);
        }
    }

}

 

 






    <framelayout android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_alignparentright="true" android:layout_height="wrap_content" android:layout_width="fill_parent"></framelayout>


 

當然最後不能忘記加入網絡權限。

 

 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved