android - ImageReader.SetOnImageAvailableLister throws cast error unless IOnImageAvailableListener is implemented by Activity -


i'm trying encapsulate taking screen shot of android screen away primary activity. have class implements

imagereader.ionimageavailablelistner 

but

 imagereader.setonimageavailablelistener(this, null) 

in class throws cast exception (but not compile exception). way i've found avoid have activity implement ionimageavailablelistner. seems there part of xamarin/mono requires argument setonimageavailablelistner of type activity implements ionimageavailablelistner.

here relevant section of class:

    public class screenshooter : imagereader.ionimageavailablelistener     {         public void takescreenshot(context context,                                             result resultcode,                                             intent data,                                             ionscreenshot onscreenshotcallback)         {             _context = context;             _onscreenshot = onscreenshotcallback;              _imageavailablecount = 0;              var size = new point();             ((activity) context).windowmanager.defaultdisplay.getsize(size);             _width = size.x;             _height = size.y;             _imagereader = imagereader.newinstance(_width, _height, imageformattype.rgb565, 2);             mediaprojectionmanager mediaprojectionmanager  = (mediaprojectionmanager) context.getsystemservice(context.mediaprojectionservice);              if (_mediaprojection == null)             {                 _mediaprojection =mediaprojectionmanager.getmediaprojection((int) resultcode, data);                 if (_mediaprojection == null)                 {                     //                    log.e(tag, "mediaprojection null. cannot take screenshot.");                     logger.error("mediaprojection null. cannot take screenshot.");                     return;                 }             }             try             {                 _virtualdisplay = _mediaprojection.createvirtualdisplay("screenshotter", _width, _height, (int) context .resources.displaymetrics.densitydpi, (displayflags) displaymanager.virtualdisplayflagautomirror, _imagereader.surface, null, null);                  /////////////   line fails    ////////////                 _imagereader.setonimageavailablelistener(this, null);                 ///////////////////////////////////////////////////////////             }             catch (exception ex)             {                 logger.error($"error in {nameof(takescreenshot)}", ex);                 throw;             }              return;         }         //ionimageavailablelistener members          public void onimageavailable(imagereader reader)         {                // stuff          }  ...    } 

i instantiate in activity requests screen shot with:

public class mainactivity : activity {     private void takescreenshot()     {         startactivityforresult(((mediaprojectionmanager) getsystemservice(mediaprojectionservice)).createscreencaptureintent(),                                request_code);     }      protected override void onactivityresult(int requestcode, result resultcode, intent data)  {      screenshooter.getinstance().takescreenshot(this, resultcode, data, callback); } 

the callback code called after screenshot completed omitted.

i'm seeing java android code examples of pattern everywhere. (specifically, i've been looking @ example: https://github.com/omerjerk/screenshotter)

i'm new both android , xamarin, have extensive experience in c#.

is bug in xamarin? there work-around?

thanks

the way i've found avoid have activity implement ionimageavailablelistner.

no, it's not way, avoid implement interface on activity class, you're doing right create own class implements interface, example:

public class myimagereaderlistener : java.lang.object, imagereader.ionimageavailablelistener {     public void dispose()     {         //todo:     }      public void onimageavailable(imagereader reader)     {         //todo:     } } 

the cast exception caused use of this, refers activity class, , class doesn't implement interface. if you're familiar c#, should able understand this, may change this here new instance of class:

imagereader.setonimageavailablelistener(new myimagereaderlistener(), null); 

for code like:

screenshotter.getinstance()              .setsize(720, 1280)              .takescreenshot(this, resultcode, data, new screenshotcallback() {                  @override                  public void onscreenshot(bitmap bitmap) {                      //enjoy bitmap                  }              }); 

c# don't have such anonymous inner class of java, may refer can c# anonymous class implement interface?. that's why created class implement interface.

the reason why lot of demos directly implement interface on activity need return value listener continue our logical work, may check api design of xamarin events , listeners.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -