安卓开发之cache 的使用(图片查看器案例)

package com.lidaochen.test;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends AppCompatActivity {
    private EditText et_path;
    private ImageView iv_pic;

    // 创建handler对象
    public Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bitmap bitmap = (Bitmap)msg.obj;
            // 设置图片到ImageView
            iv_pic.setImageBitmap(bitmap);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 找到  ImageView 和 EditText控件
        et_path = (EditText)findViewById(R.id.et_path);
        iv_pic = (ImageView)findViewById(R.id.iv_pic);
    }
    public void click(View v)
    {
        new Thread()
        {
            public void run()
            {
                try
                {
                    // 获取图片路径
                    String path = et_path.getText().toString().trim();
                    File file = new File(getCacheDir(), Base64.encodeToString(path.getBytes(), Base64.DEFAULT));
                    if (file.exists() && file.length() > 0)
                    {
                        // 使用缓存图片
                        System.out.println("使用缓存图片!");
                        final Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                        // 把cacheBitmap显示到ImageView上
                        Message msg = Message.obtain(); // 使用Message的静态方法,可以减少对象的创建
                        msg.obj = cacheBitmap;
                        handler.sendMessage(msg);
                    }
                    else
                    {
                        // Toast.makeText(getApplicationContext(), "第一次连接网络!", Toast.LENGTH_SHORT).show();
                        System.out.println("第一次连接网络!");
                        // 创建url对象
                        URL url = new URL(path);
                        //  获取HttpURLConnection对象
                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                        // 设置请求方式
                        httpURLConnection.setRequestMethod("GET");
                        // 设置超时时间
                        httpURLConnection.setReadTimeout(5000);
                        // 获取服务器返回的状态码
                        int code = httpURLConnection.getResponseCode();
                        if (code == 200)
                        {
                            // 获取图片数据,不管什么数据,都是以流的形式返回
                            InputStream in = httpURLConnection.getInputStream();
                            // 缓存图片 谷歌给我们提供了一个缓存目录
                            FileOutputStream fos = new FileOutputStream(file);
                            int len = -1;
                            byte buffer[] = new byte[1024]; // 1kB
                            while((len = in.read(buffer)) != -1)
                            {
                                fos.write(buffer, 0, len);
                            }
                            // 关闭流
                            fos.close();
                            in.close();
                            // 通过位图工厂,获取位图
                            final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                            // 创建MSG 对象
                            // Message msg = new Message();
                            Message msg = Message.obtain(); // 使用Message的静态方法,可以减少对象的创建
                            msg.obj = bitmap;
                            handler.sendMessage(msg);
                        }
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

 

转载于:https://www.cnblogs.com/duxie/p/10949668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值