Forum Xamarin.Forms

stream keeps returning null when trying to get image

i used https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker and am trying to receive an image on a android emulator and stream keeps returning null.

any help would be awesome

Best Answers

Answers

  • theConfusedOnetheConfusedOne Member ✭✭✭
    edited November 2019

    @JohnHardman said:
    @theConfusedOne

    Check whether DependencyService.Get<IPhotoPickerService>() is returning null.
    If it is returning null, then you are not registering the PhotoPickerService correctly.

    i added
    [assembly: Dependency(typeof(PhotoPickerService))] above the service but now im getting

    mainactivity

  • JohnHardmanJohnHardman GBUniversity mod

    @theConfusedOne said:
    i added
    [assembly: Dependency(typeof(PhotoPickerService))] above the service but now im getting

    Ok, that's a different problem.

    However, the exception message in your screenshot is obscuring the code so I cannot see all of the possible causes of the exception. Please post the source of GetImageStreamAsync as text within the body of a post, rather than posting screenshots that are hard to search even when not obscured.

  • theConfusedOnetheConfusedOne Member ✭✭✭

    @JohnHardman said:

    @theConfusedOne said:
    i added
    [assembly: Dependency(typeof(PhotoPickerService))] above the service but now im getting

    Ok, that's a different problem.

    However, the exception message in your screenshot is obscuring the code so I cannot see all of the possible causes of the exception. Please post the source of GetImageStreamAsync as text within the body of a post, rather than posting screenshots that are hard to search even when not obscured.

    [assembly: Dependency(typeof(PhotoPickerService))]
    namespace Kula.Droid
    {
    public class PhotoPickerService : IPhotoPickerService
    {
        public Task<Stream> GetImageStreamAsync()
        {
            // Define the Intent for getting images
            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);
    
            // Start the picture-picker activity (resumes in MainActivity.cs)
            MainActivity.Instance.StartActivityForResult(
                Intent.CreateChooser(intent, "Select Picture"),
                MainActivity.PickImageId);
    
            // Save the TaskCompletionSource object as a MainActivity property
            MainActivity.Instance.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();
    
            // Return Task object
            return MainActivity.Instance.PickImageTaskCompletionSource.Task;
        }
       }
    }
    
  • JohnHardmanJohnHardman GBUniversity mod

    @theConfusedOne

    Are you setting MainActivity.Instance to a non-null value anywhere?

  • theConfusedOnetheConfusedOne Member ✭✭✭

    @JohnHardman said:
    @theConfusedOne

    Are you setting MainActivity.Instance to a non-null value anywhere?

    i was just copy and pasting from that link. i dont think so

  • JarvanJarvan Member, Xamarin Team Xamurai

    @theConfusedOne Any updates? If you've solved the issue, please mark the helpful reply as the answer. It'll help others who face the similar problem.

  • theConfusedOnetheConfusedOne Member ✭✭✭

    @Jarvan said:
    @theConfusedOne Any updates? If you've solved the issue, please mark the helpful reply as the answer. It'll help others who face the similar problem.

    yup i keep forgetting to mark an answer

Sign In or Register to comment.