Mediaplugin: Unable to check orientation: Java.Lang.NullPointerException

Created on 12 Oct 2017  路  18Comments  路  Source: jamesmontemagno/MediaPlugin

Bug Information

Calling CrossMedia.Current.TakePhotoAsync causes exception Unable to check orientation: Java.Lang.NullPointerException

Version Number of Plugin: 3.0.1
Device Tested On: Samsung J5, Android 6.0
Version of VS: 2017, version 15.3.5
Version of Xamarin: Xamarin forms 2.4.0.283
Versions of other things you are using: Xamarin.FFImageLoading.Forms 2.2.20

Steps to reproduce the Behavior

Put the phone down on a flat surface and take a photo with camera.
Accept photo

Expected Behavior

return a black photo, as shown in the camera preview

Actual Behavior

return a file, that when trying to load cases invalid file format exception
Unable to check orientation: Java.Lang.NullPointerException: Attempt to get length of null array Unable to check orientation: Java.Lang.NullPointerException: Attempt to get length of null array 10-12 14:33:51.411 I/mono-stdout(16474): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <e499a5498b1b48379b88fe5ed629079f>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <e499a5498b1b48379b88fe5ed629079f>:0 at Android.Media.ExifInterface.SaveAttributes () [0x0000a] in <2e14bb2dd93a405e81838369ed72695b>:0 at Plugin.Media.MediaImplementation+<TakePhotoAsync>d__16.MoveNext () [0x000b2] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaPickerActivity.cs:556 --- End of managed Java.Lang.NullPointerException stack trace ---

Code snippet

` await CrossMedia.Current.Initialize();

                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    MaxWidthHeight = 500,
                    PhotoSize = PhotoSize.MaxWidthHeight,
                    CompressionQuality = 80
                });

                //Exception Unable to check orientation is shown in log

                Func<CancellationToken, Task<Stream>> fs = (t) => Task.Run(() => file.GetStream());
                var jpegstream = ImageService.Instance.LoadStream(fs).AsJPGStreamAsync();

                //This line crashes with System.BadImageFormatException: Bad image format
                var s = await jpegstream;`
bug

All 18 comments

Do you have a sample project?

Hi James.
Here is a sample app
TestPhoto.zip

@prashantvc can you validate?

I am not able to reproduce this issue on Pixel running Android 8 and Galaxy S7E running Android 7.
@rnpayet While I find the Device running Android 6, could you get the source code from this repo
and try debug?

Hi @prashantvc We are also getting this issue

I have tired on couple of phones with android 7.

Unable to check orientation: Java.Lang.NullPointerException: Attempt to get length of null array
10-16 14:04:59.650 I/mono-stdout(23114): Unable to check orientation: Java.Lang.NullPointerException: Attempt to get length of null array
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <1d799d87381c4db5b1252825fd105b84>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <1d799d87381c4db5b1252825fd105b84>:0
10-16 14:04:59.650 I/mono-stdout(23114): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
10-16 14:04:59.651 I/mono-stdout(23114): at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <1d799d87381c4db5b1252825fd105b84>:0
10-16 14:04:59.651 I/mono-stdout(23114): at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <1d799d87381c4db5b1252825fd105b84>:0
10-16 14:04:59.652 I/mono-stdout(23114): at Android.Media.ExifInterface.SaveAttributes () [0x0000a] in <484cd191a834496c85d38d0e644a42c4>:0
at Android.Media.ExifInterface.SaveAttributes () [0x0000a] in <484cd191a834496c85d38d0e644a42c4>:0
at Plugin.Media.MediaImplementation+d__16.MoveNext () [0x0047c] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:186
--- End of managed Java.Lang.NullPointerException stack trace ---
10-16 14:04:59.652 I/mono-stdout(23114): at Plugin.Media.MediaImplementation+d__16.MoveNext () [0x0047c] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:186
10-16 14:04:59.652 I/mono-stdout(23114): --- End of managed Java.Lang.NullPointerException stack trace ---
10-16 14:04:59.653 I/mono-stdout(23114): java.lang.NullPointerException: Attempt to get length of null array

Thanks for the details, I will give it another try.
Could you get the source code from this repo and try re-produce it instead of the using nuget?

The issue is not resolved. The exif is not null, what I did notice is that the Thumbnail is null exif.GetThumbnail() returns null array but the exif.HasThumbnail returns true.
The java.lang.NullPointerException is thrown inside exif.SaveAttributes();.

Awesome! Will update right now thanks for going through this.

So, there is no way for me to set that and it automatically is set. I will try catch a bit more, but not much I can do.

Yes, I think the issue is inside SaveAttributes. The try catch you add should at least allow us to get the image. Thanks James

I did some more test with the try Catch around SaveAttributes. The error corrupts that final image file. What I end up doing was adding a check to see if exif has valid thumbnail for save

if (options.SaveMetaData)
{
  if (exif != null)
  {
    if (exif.HasThumbnail && !(exif.GetThumbnail()?.Length > 0))
    {
      Console.WriteLine($"Invalid exif");
    }
    else
    {
      SetMissingMetadata(exif, options.Location);
      try
      {
         exif.SaveAttributes();
      }
      catch (Exception ex)
      {
        Console.WriteLine($"Unable to save exif {ex}");
      }
  }
}

I put this code in place and pushed out a new beta package for you to try.

I believe

exif.GetThumbnail()?.Length <= 0

will returnfalse if GetThumbnail() returns null,
shouldn't it be

!(exif.GetThumbnail()?.Length > 0)

or
(exif.GetThumbnail()?.Length ?? 0) <= 0

Yeah, would be this:

if (exif.HasThumbnail && (exif.GetThumbnail()?.Length ?? 0) <= 0)
                return false;

good catch

Thanks. Happy to help. Tested and confirm that it is now working as expected.

I can see following error with 3.1.1 release on emulator (API v25 Android version 7.1)

Unable to check orientation: Java.Lang.NullPointerException: Attempt to get length of null array
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <7cfbebb561c54efc9010b018c0846c7e>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <7cfbebb561c54efc9010b018c0846c7e>:0
at Android.Media.ExifInterface.SaveAttributes () [0x0000a] in <9ef29c909d7e4606a46b131129da3975>:0
at Plugin.Media.MediaImplementation+d__15.MoveNext () [0x00282] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:89
--- End of managed Java.Lang.NullPointerException stack trace ---
java.lang.Nul
lPointerException: Attempt to get length of null array
at java.io.FileOutputStream.write(FileOutputStream.java:296)
at android.media.ExifInterface$ByteOrderAwarenessDataOutputStream.write(ExifInterface.java:2739)
at android.media.ExifInterface.writeExifSegment(ExifInterface.java:2473)
at android.media.ExifInterface.saveJpegAttributes(ExifInterface.java:1977)
at android.media.ExifInterface.saveAttributes(ExifInterface.java:1609)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)

@lasithchandrasekara try 3.1.1.201-beta for me and let me know

Well 3.1.1.202-beta actually

Was this page helpful?
0 / 5 - 0 ratings