Android studio 37 下載網絡圖片 並顯示

主程序
package com.example.myhttpdcc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;


import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


public class MainActivity extends AppCompatActivity {

    private SeekBar mSeekBar;
    private  final String urlString = "http://192.168.43.8:280/AA.jpg";
    private ImageView mIV;
    private int fileLength;
    private Bitmap mBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSeekBar = (SeekBar)findViewById(R.id.seekbar);
        mIV = (ImageView)findViewById(R.id.image);
        new Thread(downloadRunn).start();
    }
    private Runnable downloadRunn = new  Runnable(){
        @Override
        public void run() {
            try {
                URL url = new URL(urlString);
                URLConnection connection = url.openConnection();
                connection.connect();
                fileLength = connection.getContentLength();
                InputStream in = new BufferedInputStream(connection.getInputStream());
                byte[] arr = readStream(in);
                mBitmap = BitmapFactory.decodeByteArray(arr,0,arr.length);
                connectHanlder.sendEmptyMessage(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    public  byte[] readStream(InputStream in) throws Exception{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while((count = in.read(data))!=-1){
            total+=count;
            mSeekBar.setProgress((int)(total*100)/fileLength);
            bos.write(data,0,count);
        }
        bos.close();
        in.close();
        return bos.toByteArray();
    }
    private Handler connectHanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // 更新UI,顯示圖片
            if (mBitmap != null) {
                mIV.setImageBitmap(mBitmap);// display image
            }
        }
    };
}

佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="50"/>
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:scaleType="center"/>
</LinearLayout>

添加權限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myhttpdcc">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章