Issue
Following is the code that I have implemented for scanning qr code in xamarin c#
ZXingScannerView zxing= new ZXingScannerView();
private void btnScan_Clicked(object sender, EventArgs e)
{
try
{
zxing = new ZXingScannerView {
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
BackgroundColor = Color.Orange
};
NavigationPage.SetHasNavigationBar(this, false);
zxing.OnScanResult += (result) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
zxing.IsAnalyzing = false;
zxing.IsScanning = false;
//MessagingCenter.Send(result, "Scanresult");
if (result != null)
{
await DisplayAlert("Scanned code: ", text, "OK");
}
});
};
var layout = new StackLayout();
layout.BackgroundColor = Color.Red;
layout.Children.Add(zxing);
Content = zxing;
}
catch (Exception ex)
{
throw ex;
}
}
Along with this I have given permission for the camera in androidmanifest.xml. Here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.companyname.xxxxxx" android:installLocation="auto" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="xxxxxx.Android"></application>
<application android:name="android.support.multidex.MultiDexApplication"></application>
</manifest>
before this I tried implementing the following code:
private async void btnScan_Clicked(object sender, EventArgs e)
{
try
{
ZXingScannerPage scanPage = new ZXingScannerPage();
//await Navigation.PushAsync(scanPage);
scanPage.OnScanResult += (result) =>
{
// Stop scanning
scanPage.IsScanning = false;
// Pop the page and show the result
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopAsync();
if (result != null)
{
await DisplayAlert("Scanned code: ", text, "OK");
}
});
};
await Navigation.PushAsync(scanPage);
}
catch (Exception ex)
{
throw ex;
}
}
But all the codes are working fine with android 5 but none is working for android 8.1 Can anyone please help to find me if I am missing something?
Solution
But all the codes are working fine with android 5 but none is working for android 8.1
Since Android 6.0, the app must always perform a runtime permission check. Please request the permissions that the app requires in MainAcitivity class.
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Check the tutorial:
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#requesting-system-permissions
Answered By - Jarvan Zhang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.