如何在android中使用服务显示图像

如何在android中使用服务显示图像,android,android-layout,android-service,android-imageview,android-file,Android,Android Layout,Android Service,Android Imageview,Android File,我想展示我的形象。我必须在服务中执行操作,并在UI中显示结果。我不想按照要求使用异步任务。我不知道如何继续和显示图像。请帮帮我 我的代码如下: public class Service_Photo extends Service { public Service_Photo() { } @Override public IBinder onBind(Intent intent) { throw n

我想展示我的形象。我必须在服务中执行操作,并在UI中显示结果。我不想按照要求使用异步任务。我不知道如何继续和显示图像。请帮帮我

我的代码如下:

 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?
    }
    }

在您的情况下,最好的解决方案是使用广播接收器。所以在你的活动中

 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);
    }
并在
onCreate()上初始化视图

在您的服务中,在
updateImages()中

只需发送一个带有操作“your_load_photo_action”的广播意图即可显示您的图像抱歉,无法获取您的图像。如何在imageview中显示?在哪里申报和领取@pskinkCheck如何从服务更新主线程我想这将解决您的问题。@Shadow创建一个在其UI中具有ImageView的活动,并调用setImageBitmap()否,这意味着它通过在活动中使用而不使用服务变得简单@我想从service.inside方法intent.getAction()中使用的pskink,我想单独传递imageview id就足够了吗@Ahmed Hafez或哪里可以使用setImageBitmap(位图)?检查我的编辑,您将在onCreate中定义视图,然后在收到意图后加载位图