简体   繁体   English

从 URL 下载图像并将其保存到内部存储器的最佳方法

[英]Best way to download image from URL and save it into internal storage memory

I am developing an app in which I want to download image from URL.我正在开发一个应用程序,我想在其中从 URL 下载图像。 I need to download these images at once and stored it into internal storage.我需要立即下载这些图像并将其存储到内部存储器中。 There are more than 200 images for downloading.有200多张图片可供下载。 Please tell me the best way to download these images at minimum time possible.请告诉我在尽可能短的时间内下载这些图像的最佳方式。 If any third part library is available the please tell.如果有任何第三方库可用,请告诉。

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) {
            }
        }
    );

From here you can download this library.从这里你可以下载这个库。

You can download th image from an url like this:您可以从这样的网址下载图像:

URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
   out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

And you may then want to save the image so do:然后您可能想要保存图像,这样做:

FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
fos.write(response);
fos.close();

Ok first of all implement that library into gradle of app level for load the image from url into image view: 好吧,首先将该库实现到应用程序级别的gradle中,以将图像从URL加载到图像视图中:

 implementation("com.github.bumptech.glide:glide:4.6.1")
            {
                exclude group: "com.android.support"
            }

Now write below code in your activity for load the image:: 现在在您的活动中编写以下代码以加载图像::

Glide.with(this).load("url of your image").thumbnail(Glide.with(this).load(R.drawable.loadingsingleimage)).into(imageView);

Now download it into the your device storage by implementing the following code.. 现在,通过实现以下代码将其下载到设备存储中。

 download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
                    Bitmap bitmap = drawable.getBitmap();

                    File filepath = Environment.getExternalStorageDirectory();
                    File dir = new File(filepath.getAbsolutePath() + "/MotivationalQuotes/");
                    dir.mkdir();

                    File file = new File(dir, System.currentTimeMillis() + ".png");
                    try {
                        outputStream = new FileOutputStream(file);
                    } catch (FileNotFoundException e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

                    try {
                        outputStream.flush();
                    } catch (IOException e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    Toast.makeText(getApplicationContext(), "Downloaded into MotivationalQuotes.", Toast.LENGTH_SHORT).show();
                }
                catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Image is not loaded yet...", Toast.LENGTH_SHORT).show();
                }
            }

        });
BitmapDrawable bitmapDrawable = (BitmapDrawable) holder.post_image.getDrawable();
                        Bitmap bitmap = bitmapDrawable.getBitmap();
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                        MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bitmap, "IMG_" + Calendar.getInstance().getTime(), null);

It is quite a simple job...这是一个很简单的工作......

 const actionDownloadImage = (urls) => {
        urls.map((url) => {
            const splitUrl = url.split("/");
            const filename = splitUrl[splitUrl.length - 1];
            fetch(url)
                .then((response) => {
                    response.arrayBuffer().then(function (buffer) {
                        const url = window.URL.createObjectURL(new Blob([buffer]));
                        const link = document.createElement("a");
                        link.href = url;
                        link.setAttribute("download", filename); //or any other extension
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    });
                })
                .catch((err) => {
                    console.log(err);
                });
        });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 url 下载图像并保存到内部存储 - Download image using url and save to internal storage 我要从url下载图像并将其保存到设备的内存中,然后在成功保存后播放? - I want to download image from url and save it to device's internal memory and play after successfully saved? 如何将图片从URL保存到内部或外部存储中? - How to save image from URL into internal or external storage? Android:如何在给出 Image-Url 时制作图像下载按钮并将其保存在内部存储中 - Android: How can i make an Image Download Button when Image-Url is given and save it on internal storage 通过单击图像视图中的图像,可从应用程序下载图像并保存在内部存储中 - By clicking on image in image view image download from app and save in internal storage 如何从内部存储器拍摄照片并将捕获的图像保存在android内部存储器中? - How to take picture from internal storage and save capture image in internal memory android? 将图像保存到内部存储器 - Save image to internal storage Android:在外部存储设备上保存图像的最佳方法 - Android: Best way to save image at external Storage 如何将文件从 firebase 存储 url 下载到内部存储 - How to download file from firebase storage url to internal storage android从内部存储器中的url存储下载图像,然后在imageview中显示该图像 - android download the image from url store in internal memory then display that image in imageview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM