Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android網絡HttpURLConnection抓取網絡圖片

android網絡HttpURLConnection抓取網絡圖片

編輯:關於Android編程

 

package com.liang.netpicture;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends ActionBarActivity {
    ImageView iv_main;
    private final int SUCCESS=0;
    private final int ERROR=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_main = (ImageView) findViewById(R.id.iv_main);
        //handler接受消息
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==SUCCESS){
                    iv_main.setImageBitmap((Bitmap) msg.obj);
                }else{
                    Toast.makeText(MainActivity.this,抓取失敗,Toast.LENGTH_SHORT).show();
                }
            }
        };
        //耗時操作都需要在子線程中完成,並且不能在子線程中更改UI線程的組件狀態,需使用Handler進行線程通信
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = getNetPicture();
                Message msg=new Message();
                if(bitmap!=null){
                    msg.what=SUCCESS;
                    msg.obj=bitmap;
                }else{
                    msg.what=ERROR;
                }
                handler.sendMessage(msg);
            }
        }).start();
    }
    private Bitmap getNetPicture() {
        Bitmap bitmap=null;
        int responseCode = 0;
        InputStream is = null;
        try {
            URL url = new URL(http://f.hiphotos.baidu.com/image/pic/item/3801213fb80e7beca9bfb6e02d2eb9389b506b4e.jpg);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(GET);
            conn.setConnectTimeout(10 * 1000);
            conn.setReadTimeout(5 * 1000);
            conn.connect();
            responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                is = conn.getInputStream();
                bitmap = BitmapFactory.decodeStream(is);
            }
        } catch (IOException e) {
            Log.i(test, 訪問失敗: + responseCode);
            e.printStackTrace();
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return  bitmap;
    }
}
 

 



 

 

 

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