为了账号安全,请及时绑定邮箱和手机立即绑定

利用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
提交
取消