Search code examples
javaandroidandroid-studioasynchronousandroid-asynctask

AsyncTask Alternative for background image Download? (AsyncTask ---------Deprecated)


As AsyncTask has been Deprecated. I want to Download image in background but i don't know how implement async Functionality without extending to AsyncTask. I need some alternative way to Download image in Background

package com.developoNetwork.ImageDownload;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

     ImageView imageView;

    public  void imageDownload(View view){
        ImageDownloader imageDownloader = new ImageDownloader("https://i.pinimg.com/originals/ca/76/0b/ca760b70976b52578da88e06973af542.jpg");

        try {
          imageDownloader.download();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
    }



//Download Image
    public class ImageDownloader{
       String urls;
       ImageDownloader(String url){
           urls = url;
       }


        public void download() {
            System.out.println("Start");
       

               try {
                   Logger.getLogger("Start");
//                  ImageView  imageView = findViewById(R.id.imageView);
                   URL url = new URL(urls);
                   HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                   InputStream inputStream = connection.getInputStream();
                   Bitmap image = BitmapFactory.decodeStream(inputStream);
                   imageView.setImageBitmap(image);
                   
                   Logger.getLogger("Done");

               } catch (MalformedURLException e) {
                   System.out.println("Error Malformed");
                   e.printStackTrace();
               } catch (IOException e) {
                   System.out.println("Exception");
                   e.printStackTrace();
               }





        }
    }
}

QUESTION How can i achieve Asynchronous Behavior. As it is deprecate Now. Is there alternative in android development to Download image in background.

I read the solution on stackoverflow of using thread for background task but it crash the App when i press a button to Download the image.


Solution

  • For running background tasks you have

    • MutliThreading
    • Rx Java
    • Coroutines (Kotlin)

    among the above three coroutines is preferred as it is easy to learn and more efficient