【问题标题】:How to display an image using service in android如何在android中使用服务显示图像
【发布时间】:2014-02-27 08:43:51
【问题描述】:

我想展示图片。我必须在服务中执行操作并在 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?
    }
    }

【问题讨论】:

  • startActivity() 将显示您的图像
  • 抱歉找不到你。如何在 imageview 中显示?在哪里申报和领取? @pskink
  • 检查如何从服务更新主线程,我猜它会解决你的问题。
  • @Shadow 创建一个在其 UI 中有一个 ImageView 的 Activity 并调用 setImageBitmap()
  • 不,这意味着通过在活动中使用而不使用服务变得直截了当。 @pskink 我想从服务中使用。

标签: android android-layout android-service android-imageview android-file


【解决方案1】:

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

 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”操作发送广播意图

【讨论】:

  • 在intent.getAction()方法中我想单独传递imageview id就够了吗? @Ahmed Hafez 或者我在哪里可以使用 setImageBitmap(Bitmap)?
  • 检查我的编辑,您将在 onCreate 中定义您的视图,然后在收到意图后加载您的位图
猜你喜欢
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多