Binding Captured Image in ImageView it shows NullReferenceException in Xamarin Android

MohanasundharamMohanasundharam Member ✭✭✭
edited April 23 in Xamarin.Android

//Permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

//for open camera

private void goCamera(object sender,EventArgs args)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, REQUEST_TAKE_PHOTO);
}

//for handle the result

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Bitmap bitMap = (Bitmap)data.Extras.Get("data");
pictureViewer.SetImageBitmap(bitMap);
}

Here the data(Intent) result is getting null, how to resolve this issue?

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    I created a new app and it worked properly.The effect is as follows:

    Maybe you faced a permission issue. An app that targets Android 6.0 or higher must always perform a runtime permission check.
    You can refer to the following link to add a runtime permission check.
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=vswin

    Besides, you can use the package Permissions.Plugin

    For more details: https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

  • MohanasundharamMohanasundharam Member ✭✭✭

    I have checked all the permissions and all the things, i think all are ok. But still it was not binding the captured image in ImageView

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 24

    Hi, @Mohanasundharam
    Could you please post a basic demo so that we can test with it?

  • MohanasundharamMohanasundharam Member ✭✭✭
    edited April 25

    Yes. I need the output like your first reply for this question

  • jezhjezh Member, Xamarin Team Xamurai

    What do you mean by the words I need the output like your first reply for this question ?

  • MohanasundharamMohanasundharam Member ✭✭✭

    yes, you asked me a basic demo right, that's why i said like that, is there any wrong understanding, sorry for that :neutral:

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 26

    Well, I just asked you for a basic demo to test . I guess our code should be different in some place.

  • jezhjezh Member, Xamarin Team Xamurai

    I have created a simple demo which runs properly.
    The whole app is in the attachment you can check it.

  • MohanasundharamMohanasundharam Member ✭✭✭

    Well, Thank you @jezh

  • MohanasundharamMohanasundharam Member ✭✭✭
    edited April 26

    I have an another issue @jezh, which i installed this app in new device it doesn't show the runtime permission pop-up and automatically it enabled.

    the log is,

    04-26 18:47:44.647 W/ActivityThread(11712): handleWindowVisibility: no activity for token android.os.[email protected]
    Resolved pending breakpoint at 'MainPagePicCamera.cs:78,1' to void POC.Pic_Camera.MainPagePicCamera.OnActivityResult (int requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00035].
    Resolved pending breakpoint at 'MainPagePicCamera.cs:80,1' to void POC.Pic_Camera.MainPagePicCamera.OnActivityResult (int requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00036].
    Resolved pending breakpoint at 'MainPagePicCamera.cs:85,1' to void POC.Pic_Camera.MainPagePicCamera.OnActivityResult (int requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x0005f].
    04-26 18:47:44.797 W/POC.POC (11712): JNI RegisterNativeMethods: attempt to register 0 native methods for md5eae3ad6ed1b7f6713979aa7524906105.CircleImageView
    04-26 18:47:44.799 D/Mono (11712): DllImport searching in: '__Internal' ('(null)').
    04-26 18:47:44.799 D/Mono (11712): Searching for 'java_interop_jnienv_get_object_array_element'.
    04-26 18:47:44.799 D/Mono (11712): Probing 'java_interop_jnienv_get_object_array_element'.
    04-26 18:47:44.800 D/Mono (11712): Found as 'java_interop_jnienv_get_object_array_element'.
    04-26 18:47:44.820 D/Mono (11712): DllImport searching in: '__Internal' ('(null)').
    04-26 18:47:44.820 D/Mono (11712): Searching for 'java_interop_jnienv_new_local_ref'.
    04-26 18:47:44.820 D/Mono (11712): Probing 'java_interop_jnienv_new_local_ref'.
    04-26 18:47:44.820 D/Mono (11712): Found as 'java_interop_jnienv_new_local_ref'.
    04-26 18:47:44.876 I/POC.POC (11712): Compiler allocated 4MB to compile void android.widget.TextView.(android.content.Context, android.util.AttributeSet, int, int)
    04-26 18:47:47.636 D/Mono (11712): Assembly Ref addref POC[0x74866dce80] -> Xamarin.Android.Support.Compat[0x747c8ab000]: 4
    04-26 18:47:47.637 I/Pic Camera(11712): Show camera button pressed. Checking permission.
    04-26 18:47:47.640 I/Pic Camera(11712): ************CAMERA permission has already been granted. Displaying camera preview.
    04-26 18:47:47.981 D/OpenGLRenderer(11712): endAllActiveAnimators on 0x7467915f00 (MenuPopupWindow$MenuDropDownListView) with handle 0x7467968c20
    No Picture Captured!
    04-26 18:47:53.759 I/POC.POC (11712): CollectorTransition concurrent copying GC freed 9578(547KB) AllocSpace objects, 4(208KB) LOS objects, 49% free, 1928KB/3MB, paused 63us total 163.923ms
    04-26 18:47:53.780 D/Mono (11712): DllImport attempting to load: '/system/lib64/liblog.so'.
    04-26 18:47:53.781 D/Mono (11712): DllImport loaded library '/system/lib64/liblog.so'.
    04-26 18:47:53.781 D/Mono (11712): DllImport searching in: '/system/lib64/liblog.so' ('/system/lib64/liblog.so').
    04-26 18:47:53.781 D/Mono (11712): Searching for '__android_log_print'.
    04-26 18:47:53.781 D/Mono (11712): Probing '__android_log_print'.
    04-26 18:47:53.781 D/Mono (11712): Found as '__android_log_print'.
    04-26 18:47:53.782 I/mono-stdout(11712): No Picture Captured!

    and the captured result(intent data) value is null, which means the data is null, so the result like "No Picture Captured!"

  • jezhjezh Member, Xamarin Team Xamurai

    What Android device are you using? What is the API version?

  • MohanasundharamMohanasundharam Member ✭✭✭
    edited April 30 Accepted Answer

    I just solved this problem with Storage, by using this tutorial, https://c-sharpcorner.com/article/xamarin-android-create-android-own-camera-app/

  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, and thanks for sharing the solution.

Sign In or Register to comment.