第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

異步任務(wù)實(shí)現(xiàn)網(wǎng)上下載圖片并存儲(chǔ)在sdcard上,并將下載的圖片顯示在屏幕上

標(biāo)簽:
Android

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:="download"   android:layout_width="match_parent" android:layout_height="wrap_content"   android:text="图片下载">

    <imageview android:id="@+id/iv"   android:layout_height="wrap_content" android:layout_width="wrap_content"   android:layout_gravity="center">

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

 

2.MainActivity.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

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

package com.example.day12_ex_01;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

 

import android.app.Activity;

import android.app.ProgressDialog;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

    ProgressDialog pd;

    @Override

    protected void onCreate(Bundle   savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        pd=new ProgressDialog(this);

        pd.setTitle("图片下载");

        pd.setMessage("正在下载.....");

        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        pd.setIndeterminate(false);

        pd.setCancelable(false);

        pd.setMax(100);

     }

 

    public void download(View   view){

        pd.show();

        new MyTask().execute();

    }

    class MyTask extends AsyncTask<void,integer,void>{

 

        @Override

        protected   Void doInBackground(Void... params) {

             try   {

                URL   url=new URL("http://www.ytmfdw.com/image/img3.jpg");

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

                InputStream   in=conn.getInputStream();

                String   path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/a.jpg";

                File   file=new File(path);

                OutputStream   out=new FileOutputStream(file);

                int   totals=conn.getContentLength();

                int   sumCount=0;

                byte   buffer[]=new byte[1024];

                int   len=-1;

                while((len=in.read(buffer))!=-1){

                    out.write(buffer,0,len);

                    sumCount+=len;

                    float   per=sumCount*100f/totals;

                    publishProgress((int)per);

                }

                out.flush();

                out.close();

                in.close();

                conn.disconnect();

             }   catch (Exception e) {

                //   TODO Auto-generated catch block

                e.printStackTrace();

            }

            return   null;

        }

        @Override

        protected   void onProgressUpdate(Integer... values) {

            //   TODO Auto-generated method stub

            super.onProgressUpdate(values);

            pd.setProgress(values[0]);

        }

        @Override

        protected   void onPostExecute(Void result) {

            //   TODO Auto-generated method stub

            super.onPostExecute(result);

            if(pd!=null&&pd.isShowing()){

                pd.dismiss();

            }

             String   path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/a.jpg";

             ImageView   iv=(ImageView) findViewById(R.id.iv);

             Bitmap   bt=BitmapFactory.decodeFile(path);

             iv.setImageBitmap(bt);

        }

    }

 

}</void,integer,void>

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

 


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

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

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消