1

This is my JSON Response from server

This clearly shows that the media file urls are recieved onto the client side after getting the response . My Question is how do i download the files using the urls which i don't have access to until i get the response? And Is there any possibility to download the file at the same time while getting the url or else to recieve the url and then download the file?

I am stuck here and can anyone briefly explain me the solution here?

Any help is appreciated!

Dilip Krishna
  • 139
  • 3
  • 16

1 Answers1

1

You need parse the Json in a model.

Then you can get the url property and download it to the file system. You can use HttpUrlConnection to do this or if you want use the Picasso library with Target Picasso and download it in the file system.

Let me show you an example.

Using Picasso:

        public static void downloadImageWithPicasso(Context context, String url) {
            Picasso.with(context)
                    .load(your_url_from_model)
                    .into(saveUsingTarget(url));
        }

        private static Target saveUsingTarget(final String url){
            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() + "/" + url);
                            try {
                                file.createNewFile();
                                FileOutputStream ostream = new FileOutputStream(file);
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                                ostream.flush();
                                ostream.close();
                            } catch (IOException e) {
                                Log.e("IOException", e.getLocalizedMessage());
                            }
                        }
                    }).start();

                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            };
            return target;
        }

Using HttpUrlConnection

public void downloadImageWithUrlConnection(){
        try{

            URL url = new URL("your_url_from_model");

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            File sdCardPath = Environment.getExternalStorageDirectory().getAbsoluteFile();

            String filename = "file_name.png";


            File file = new File(sdCardPath,filename);
            file.createNewFile();

            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();

            byte[] buffer = new byte[1024];
            int bufferLength = 0;

            while ( (bufferLength = inputStream.read(buffer)) > 0 ){
                fileOutput.write(buffer, 0, bufferLength);

            }

            fileOutput.close();

        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

IMPORTANT: If you want use the HttpURLConnection options, you need run this in a background thread.

I hope this helps you.

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
  • I am using Retrofit for network operations, And I am using Glide for image display,Will Glide be useful or else i have to change for picasso – Dilip Krishna Sep 06 '18 at 14:44
  • If you want keep using Glide you have this question: https://stackoverflow.com/questions/44761720/save-pictures-in-device-glide-library I hope this helps you – Juanjo Berenguer Sep 06 '18 at 14:47