Download Images From URL to SD Card

前端 未结 5 387
不知归路
不知归路 2020-12-14 13:12

I am trying to create a very simple Image Downloading app. in which i want to download all images from this url to sd card: https://www.dropbox.com/sh/5be3kgehyg8uzh2

5条回答
  •  时光说笑
    2020-12-14 14:00

    Use Picasso and load into a Target

    I agree with Ichigo Kurosaki's answer above. Here is a detailed example of how you can use Picasso and a Picasso Target.


    How you call the Picasso code

    Picasso.with(ImageDetailActivity.this).load(
    galleryObjects.get(mViewPager.getCurrentItem()).fullImagePath).into(target);
    


    Picasso Target example

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {
    
                    File file = new File(
                        Environment.getExternalStorageDirectory().getPath() 
                        + "/saved.jpg");
                    try {
                            file.createNewFile();
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                            ostream.close();
                    }
                    catch (Exception e) {
                            e.printStackTrace();
                    }
                }
            }).start();
        }
    
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {}
    
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {}
    };
    

提交回复
热议问题