Android下载网络图片,ImageView读取本地图片

话不多说,也是android开发的初学者.这篇博客就是:第一次开启应用根据Url下载网络图片到本地文件夹,之后开启应用直接获取本地文件显示到ImageView.想着实现android的第二级硬盘缓存.第一次写,写的不好见谅.

首先在manifest文件中添加权限:

<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- 操作外部存储设备文件权限(SD卡) -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

布局文件中只加了一个ImageView,这里就不贴出来了.

MainActivity代码如下:
public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private String path;
    private String fileName;
    private String imgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //绑定ImageView
        img = (ImageView) findViewById(R.id.img);
        
        File imgFile = new File(Environment.getExternalStorageDirectory() + "/LALA/");

        path = Environment.getExternalStorageDirectory() + "/LALA/";
        //图片的Url
        imgUrl = "http://cdnq.duitang.com/uploads/item/201406/15/20140615203435_ABQMa.jpeg";
        //定义图片的文件名 为 20140615203435_ABQMa.jpeg
        fileName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);

        //如果文件路径不存在
        if (!imgFile.exists()) {
            //在外部存储其中创建如下路径
            imgFile.mkdirs();
            
            //第一次显示网络图片
            //Picasso网络图片解析工具 需要加对应依赖如下
            //compile 'com.squareup.picasso:picasso:2.3.2'
            Picasso.with(this).load(imgUrl).into(img);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(imgUrl);
                        try {
                            URLConnection connection = url.openConnection();

                            connection.connect();
                            InputStream inputStream = connection.getInputStream();

                            long fileSize = connection.getContentLength();
                            if (fileSize <= 0) throw new RuntimeException("无法获知文件大小");

                            //这里
                            if (inputStream != null) {
                                File file1 = new File(path);
                                File file2 = new File(path + fileName);
                                //如果文件夹不存在,创建文件夹
                                if (!file1.exists()) {
                                    file1.mkdirs();
                                }
                                //如果图片文件不存在创建图片文件(注意此时为空的图片文件)
                                if (!file2.exists()) {
                                    file2.createNewFile();
                                }

                                FileOutputStream fos = new FileOutputStream(file2);

                                //定义字节数组 每次1024位 循环将inputStream图片信息写到fos中
                                byte[] bytes = new byte[1024];
                                do {
                                    int info = inputStream.read(bytes);
                                    if (info == -1) {
                                        break;
                                    }
                                    fos.write(bytes, 0, info);

                                } while (true);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } 
        //如果路径存在,说明图片下载成功 走这里
        else {
            try {
                Log.d("MainActivity", "第一次之后都会走这里");
                //找到路径下的图片文件 转化为bitmap类型 显示在ImageView上
                FileInputStream fileInputStream = new FileInputStream(path + fileName);
                Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
                img.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

好了,整个需求就结束了.需求很简单,希望能帮到大家~





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值