0

I want to set bitmap for ImageView. I have the URL from Firebase stroage, I use the BitmapFactory to turn to picture, I need to apply it on ImageView, I can do it with AsyncTask but I don't know how to did it with thread or runnable.

AsyncTask cannot use anymore in Android API 30, after I find on Internet, I find a solution that is run with thread and runnable.

These are my code that can work with AsyncTask:

AsyncTask<Void,Void,Bitmap> work = new AsyncTask<Void, Void, Bitmap>() {
    @Override
    protected Bitmap doInBackground(Void... voids) {
        URL url = null;
        try {
            url = new URL("Some URL");
        } catch (MalformedURLException e) {
            Log.d("Error",e.toString());
        }
        Bitmap bmp = null;
        try {
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (IOException e) {
            Log.d("Error",e.toString());
        }
        return bmp;
    }

    @Override
    protected void onPostExecute(Bitmap bmp){
        imageMessage.setImageBitmap(bmp);
    }
};
work.execute();

I try to do it with thread and runnable but I don't know after I docode, how do I sent and apply it on ImageView. In AsyncTask, I can do that.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Jeff Li
  • 3
  • 1
  • You can refer to [the possible alternatives here](https://stackoverflow.com/questions/58767733/the-asynctask-api-is-deprecated-in-android-11-what-are-the-alternatives) as a starting point. – Enowneb Jan 31 '23 at 08:52

1 Answers1

1

I hope this would help :

public interface Operation {
       void operate(Bitmap data);
       void fail();
}

here the function/method for request url

void testRun(Operation operation) {
    new Thread(() -> {
        // do background stuff here
        URL url = null;
        try {
            url = new URL("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRZs3rtkdQK-JyQpUuwICb1ykMoAfdZ2V0fwOoXtJF4Hw&s");
        } catch (MalformedURLException e) {
            Log.d("Error", e.toString());
        }
        Bitmap bmp = null;
        try {
            if (url != null) {
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                operation.operate(bmp);
            } else {
               operation.fail();
             }
        } catch (IOException e) {
             operation.fail();
            Log.d("Error", e.toString());
        }
    }).start();
}

How you call it:

Testing testing = new Testing();
    testing.testRun(new Operation() {
        @Override
        public void operate(Bitmap data) {
            // If you need to set it to view
            // You have to run it in main thread
            // Since Runnable is run in background

            // example in activity either fragment
             runOnUiThread(()->{
               imageMessage.setImageBitmap(bmp); // set image here            
             });
        }
            @Override
            public void fail() {
                
            }
    });

if you want to explore more check here

Nrohpos
  • 283
  • 2
  • 7