How to display image using service in android

I want to show an image. where I have to perform maintenance operations and display the result in the user interface. I do not want to use the async task as required. I do not know how to continue and display the image. Please help me.

My code is as follows:

 public class Service_Photo extends Service {

        public Service_Photo() {

        }

        @Override

        public IBinder onBind(Intent intent) {

            throw new UnsupportedOperationException("Not yet implemented");

        }

        @Override

        public void onCreate() {
            super.onCreate();    
            Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

        }

        @Override

        public void onStart(Intent intent, int startId) {

            // For time consuming an long tasks you can launch a new thread here...

            Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

            updateImages();

        }
        private void updateImages()
        {
            String baseDir =  Environment.getExternalStorageDirectory().getAbsolutePath();
            String fileName = "am/a.jpg";
            File f = new File(baseDir + File.separator + fileName);
            if(f.exists()){

                Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
            //     img = (ImageView)findViewById(R.id.image);
            // img.setImageBitmap(myBitmap);
                Toast.makeText(getApplicationContext(), "Hello"+myBitmap.toString(), Toast.LENGTH_SHORT)
                .show();

            }
        }



        @Override

        public void onDestroy() {

            Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

        }

       public class ImageDisplay extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            //how to display the operation performed in service and display in image from service?
    }
    }
+4
source share
1 answer

The best solution in your case is to use broadcast receivers. So, in your work

 BroadcastReceiver mReceiver = new BroadcastReceiver(onReceive(Context context, Intent intent)
    {
    if(intent.getAction().equals("your_load_photo_action"))
    {
           ImageDisplay.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.setImageBitmap("YourImage");
            }
            });
    }
    });

    @Override
    public void onResume()
    {
     LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
          new IntentFilter("your_load_photo_action"));
    }

    @Override
    public void onPause()
    {
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
    }

and on your onCreate()initialize your view

, updateImages() "your_load_photo_action"

0

Source: https://habr.com/ru/post/1529164/


All Articles