c# - Why is the imageview returning null or not retrieving the selected photo from gallery? (Xamarin VS 2015 Android) -


this stressing me out day because cannot seem figure out why image isn't showing after select image gallery (of android phone). gives me full blank photo without actual image although tools (button, textview, etc.) below image move down. based code here selecting gallery image , in other threads still gives me null.

here codes:

androidmanifest.xml

<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-permission android:name="android.permission.camera" android:required="false"/> 

main.axml

<imageview             p1:src="@android:drawable/ic_menu_gallery"             p1:layout_width="fill_parent"             p1:layout_height="300.0dp"             p1:id="@+id/imageview1" /> <button             p1:text="attach photo"             p1:layout_width="wrap_content"             p1:layout_height="wrap_content"             p1:layout_gravity="center_horizontal"             p1:id="@+id/button1" /> 

mainactivity.cs (i omitted unrelated codes question.)

public static readonly int32 request_camera = 0; public static readonly int32 select_file = 1; private imageview _imageview;  protected override void oncreate(bundle bundle)         {             base.oncreate(bundle);              setcontentview(resource.layout.main);              if (isthereanapptotakepictures())             {                 createdirectoryforpictures();                  button button = findviewbyid<button>(resource.id.button1);                 _imageview = findviewbyid<imageview>(resource.id.imageview1);                 button.click += buttononclick;             }         }  private void buttononclick(object sender, eventargs eventargs)         {             string[] items = { "take photo", "choose library", "cancel" };              using (var dialogbuilder = new alertdialog.builder(this))             {                 dialogbuilder.settitle("add photo");                 dialogbuilder.setitems(items, (d, args) => {                     //take photo                     if (args.which == 0)                     {                         takeapicture(sender, eventargs);                     }                     //choose gallery                     else if (args.which == 1)                     {                         intent intent = new intent(intent.actionpick, mediastore.images.media.externalcontenturi);                         intent.settype("image/*");                         intent.setaction(intent.actiongetcontent);                         startactivityforresult(intent.createchooser(intent, "select picture"), select_file);                     }                 });                  dialogbuilder.show();             }         }  protected override void onactivityresult(int requestcode, result resultcode, intent data)         {             base.onactivityresult(requestcode, resultcode, data);              if (resultcode == result.ok)             {                 if (requestcode == request_camera)                 {                     intent mediascanintent = new intent(intent.actionmediascannerscanfile);                     android.net.uri contenturi = android.net.uri.fromfile(app._file);                     mediascanintent.setdata(contenturi);                     sendbroadcast(mediascanintent);                      // display in imageview. resize bitmap fit display.                     // loading full sized image consume memory                     // , cause application crash.                      int height = resources.displaymetrics.heightpixels;                     int width = _imageview.height;                     app.bitmap = app._file.path.loadandresizebitmap(width, height);                     if (app.bitmap != null)                     {                         _imageview.setimagebitmap(app.bitmap);                         app.bitmap = null;                     }                      // dispose of java side bitmap.                     gc.collect();                 }                 else if ((requestcode == select_file) && (data != null))                 {                     android.net.uri uri = data.data;                     _imageview.setimageuri(uri);                 }             }          } 

i posted earlier in xamarin's forum don't seem know either(?) or question vague(?).

your _imageview defined @ activity class level null within theonactivityresult method .

you can set via:

_imageview = findviewbyid<imageview> (resource.id.imageview1); 

also can use data.data intent parameter directly uri , not have create uri object hold it.

try code:

protected override void onactivityresult(int requestcode, result resultcode, intent data) {    base.onactivityresult(requestcode, resultcode, data);    _imageview = findviewbyid<imageview> (resource.id.imageview1);     if (resultcode == result.ok)    ...     else if ((requestcode == select_file) && (data != null))    {         _imageview.setimageuri(data.data);    } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -