亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

利用HttpURLConnection實現從網上獲取圖片資源并顯示在屏幕上

標簽:
Android

5bbed0a4000168b903130501.jpg

1.布局文件:

[代码]xml代码:

?

1

2

3

4

5

<linearlayout 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:orientation="vertical">

 

    <button android:id="@+id/btn_01" android:layout_width="match_parent" android:layout_height="wrap_content" android:="start" android:text="获取网上图片">

    <imageview android:id="@+id/iv_01" android:layout_height="wrap_content" android:layout_width="match_parent">

</imageview></button></linearlayout>

 

2.java文件

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

package com.example.day06_ex_02;

 

import java.io.FileInputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

 

import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.view.Menu;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

 

public class MainActivity   extends Activity   {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

     public class MyTask extends AsyncTask<string,string,bitmap deep="9">{

         Bitmap   bit;

         @Override

        protected Bitmap doInBackground(String... params) {

            try {

                //网上获取图片的地址

                URL   url=new URL("http://news.hhtc.edu.cn/uploads/20131101160529587.jpg");

                HttpURLConnection   conn=(HttpURLConnection) url.openConnection();

              bit=BitmapFactory.decodeStream(conn.getInputStream());

            }   catch (Exception   e) {

                e.printStackTrace();

            }

            return bit;

        }

          

         @Override

        protected void onPostExecute(Bitmap result) {

            //   TODO Auto-generated method stub

            super.onPostExecute(result);

            ImageView   tv=(ImageView) findViewById(R.id.iv_01);

            tv.setImageBitmap(result);

             

        }

          

     }

     public void start(View v){

         new MyTask().execute();

     }

 

}</string,string,bitmap>

 

3.打开网络权限

[代码]xml代码:

?

1

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

 

原文链接:http://www.apkbus.com/blog-813041-61090.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消