Android URL访问网络资源,通过URL加载网络图片

这里写图片描述

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<?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"
    tools:context="shortcut.song.com.myapplication.UrlTestActivity">
    <ImageView
        android:id="@+id/url_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
package shortcut.song.com.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class UrlTestActivity extends AppCompatActivity {

    ImageView imageView;
    Bitmap bitmap;

   Handler handler = new Handler(){
       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
           if (msg.what == 0x1234) {
               imageView.setImageBitmap(bitmap);
           }
       }
   };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_url_test);
        imageView = (ImageView)findViewById(R.id.url_show);

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    // 定义一个URL对象
                    URL url = new URL("http://192.168.8.27/files/JPG/9.jpg");
                    // 打开该URL对应的资源输入流
                    InputStream is = url.openStream();
                    // 从InputStream中解析出图片
                    bitmap = BitmapFactory.decodeStream(is);
                    // 发送消息,通知UI组件加载图片
                    handler.sendEmptyMessage(0x1234);
                    is.close();
                    // 再次打开该URL对应的资源输入流
                    is = url.openStream();
                    // 打开文件对应的输出流
                    OutputStream os = openFileOutput("9.jpg", MODE_PRIVATE);
                    byte[] buff = new byte[1024];
                    int hasRead = 0;
                    // 将URL对应的资源下载到本地
                    while ((hasRead = is.read(buff)) > 0){
                        os.write(buff, 0, hasRead);
                    }
                    is.close();
                    os.close();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();


    }
}

运行效果(通过URL加载的图片):

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongYuLong的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值