android網路HttpURLConnection抓取網狀圖片

來源:互聯網
上載者:User

標籤:httpurlconnection

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;    }}
需要在資訊清單檔中加入

<!--     訪問網路的許可權 -->
    <uses-permission android:name="android.permission.INTERNET"/>



android網路HttpURLConnection抓取網狀圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.