网络图片浏览器案例

1.意图:通过输入图片网址将图片展示到页面上。

2.案例的文件目录,如图:


3.布局文件activity_main.xml中,如图:


代码:
<?xml version =“1.0”encoding =“utf-8”?>
<RelativeLayout xmlns:android =“http://schemas.android.com/apk/res/android”
    的xmlns:工具= “http://schemas.android.com/tools”
    机器人:ID = “@ + ID / activity_main”
    机器人:layout_width = “match_parent”
    机器人:layout_height = “match_parent”
    工具:上下文= “com.example.bzu.picture.MainActivity”>

    <ImageView的
        机器人:layout_margin = “50dp”
        机器人:layout_width = “WRAP_CONTENT”
        机器人:layout_height = “WRAP_CONTENT”
        机器人:layout_above = “@ + ID / EDITTEXT”
        机器人:layout_centerHorizo​​ntal = “真”
        机器人:layout_marginBottom = “166dp”
        android:id =“@ + id / imageView”/>


    <的EditText
        机器人:layout_width = “WRAP_CONTENT”
        机器人:layout_height = “WRAP_CONTENT”
        安卓的inputType = “textPersonName”
        机器人:文字= “http://p1.so.qhimgs1.com/sdr/1365_768_/t012f51a8178d6b35bd.jpg”
        机器人:EMS = “10”
        机器人:layout_marginBottom = “95dp”
        机器人:ID = “@ + ID / EDITTEXT”
        机器人:layout_alignParentBottom = “真”
        机器人:layout_centerHorizo​​ntal = “真”>
        <requestFocus的/>
    </的EditText>

    <按钮
        机器人:文字= “浏览”
        安卓的onClick =“点击”
        机器人:背景=“@空”
        机器人:layout_width = “WRAP_CONTENT”
        机器人:layout_height = “WRAP_CONTENT”
        机器人:layout_alignParentBottom = “真”
        机器人:layout_centerHorizo​​ntal = “真”
        机器人:layout_marginBottom = “19dp”
        android:id =“@ + id / button”/>

</ RelativeLayout的>

4.MainActivity.java代码部分:
包com.example.bzu.picture;

导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.Handler;
导入android.os.Message;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.util.Base64;
导入android.view.View;
导入android.widget.EditText;
导入android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

公共课MainActivity扩展AppCompatActivity {
    私人ImageView imageView;
    私人EditText editText;

    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg){
            Bitmap bm =(Bitmap)msg.obj;
            imageView.setImageBitmap(BM);
        }
    };

    @覆盖
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        imageView =(ImageView)findViewById(R.id.imageView);
        editText =(EditText)findViewById(R.id.editText);
    }

    public void click(查看视图){
        新Thread(){
            public void run(){
                尝试{
                    // 1。获取图片的路径
                    String path = editText.getText()。toString()。trim();
                    // getCacheDir():返回文件系统上特定于应用程序缓存目录的绝对路径。
                    // Base64编码:网络上最常见的用于传输8位字节代码的编码方式之一
                    File file = new File(getCacheDir(),Base64.encodeToString(path.getBytes(),Base64.DEFAULT));
                    if(file.exists()&& file.length()> 0){// exists():返回一个布尔值,指示是否可以在基础文件系统上找到该文件。
                        的System.out.println( “使用缓存图片”);
                        //位图类参考:HTTP://www.cnblogs.com/jenson138/p/4302073.html
                        Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); // getAbsolutePath();返回使用该文件的绝对路径构造的新文件
                        //把cacheBitmap显示到IV上
                        消息msg = Message.obtain();
                        msg.obj = cacheBitmap;
                        handler.sendMessage(MSG);
                    } else {
                        //第一次访问联网获取数据
                        的System.out.println( “第一次访问连接网络”);

                        // 2。获取URL对象
                        URL url = new URL(path);
                        // 3。获取HttpsURLConnection的对象
                        HttpURLConnection connection =(HttpURLConnection)url.openConnection();
                        // 4。设置请求头信息
                        connection.setRequestMethod( “GET”);
                        connection.setReadTimeout(5000);
                        // 5。获取状态吗
                        int code = connection.getResponseCode();
                        if(code == 200){
                            InputStream in = connection.getInputStream();
                            //6.1缓存图片

                            FileOutputStream fos = new FileOutputStream(file);
                            int len = -1;
                            byte [] buffer = new byte [1024];
                            while((len = in.read(buffer))!= -1){
                                fos.write(buffer,0,len);
                            }
                            fos.close();
                            附寄();
                            // 7。通过位图工厂获取的位图(位图)
                            // BitmapFactory类:创建来自不同源的位图对象,包括文件,流和字节数组。
                            // decodeFile():将文件路径解码为位图如果指定的文件名为NULL,或无法解码成位图,则函数返回空
                            Bitmap Bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                            // 8。把位图显示到imagView上
                            消息msg = Message.obtain();
                            msg.obj =位图
                            handler.sendMessage(MSG);
                        }
                    }
                } catch(Exception e){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
            }
        }。开始();
    }
}

5. AndroidManifest.xml代码部分:
<?xml version =“1.0”encoding =“utf-8”?>
<manifest xmlns:android =“http://schemas.android.com/apk/res/android”
    包= “com.example.bzu.picture”>
    <uses-permission android:name =“android.permission.ACCESS_NETWORK_STATE”/>
    <uses-permission android:name =“android.permission.INTERNET”/>

    <应用
        机器人:allowBackup = “真”
        机器人:图标= “@ mip映射/ ic_launcher”
        机器人:标签= “@字符串/ APP_NAME”
        机器人:supportsRtl = “真”
        机器人:主题= “@风格/ AppTheme”>
        <activity android:name =“。MainActivity”>
            <意图滤波器>
                <action android:name =“android.intent.action.MAIN”/>

                <category android:name =“android.intent.category.LAUNCHER”/>
            </意图滤波器>
        </活动>
    </应用>

</清单>
6.运行效果,如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值