从URL下载图像并将其保存到内部存储存储器的最佳方法 [英] Best way to download image from URL and save it into internal storage memory

查看:119
本文介绍了从URL下载图像并将其保存到内部存储存储器的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个要从URL下载图像的应用程序.我需要立即下载这些图像并将其存储到内部存储器中.有200多个图像可供下载.请告诉我最好的方式以最短的时间下载这些图像.如果有任何第三方库可用,请告知.

I am developing an app in which I want to download image from URL. I need to download these images at once and stored it into internal storage. There are more than 200 images for downloading. Please tell me the best way to download these images at minimum time possible. If any third part library is available the please tell.

推荐答案

考虑将Picasso用于您的目的.我在我的一个项目中使用它.要将图像保存到外部磁盘上,可以使用以下命令:

Consider using Picasso for your purpose. I'm using it in one of my project. To save image on external disk you can use following:

 Picasso.with(mContext)
        .load(ImageUrl)
        .into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                try {
                    String root = Environment.getExternalStorageDirectory().toString();
                    File myDir = new File(root + "/yourDirectory");

                    if (!myDir.exists()) {
                        myDir.mkdirs();
                    }

                    String name = new Date().toString() + ".jpg";
                    myDir = new File(myDir, name);
                    FileOutputStream out = new FileOutputStream(myDir);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                    out.flush();
                    out.close();                        
                } catch(Exception e){
                    // some action
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }
        }
    );

从此处,您可以下载该库.

这篇关于从URL下载图像并将其保存到内部存储存储器的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆