Home > Enterprise >  .Net Maui: StorageWrite permission stops working with .net 7 on Android emulator
.Net Maui: StorageWrite permission stops working with .net 7 on Android emulator

Time:11-09

It worked on .NET 6, but now when I migrated my .net maui project to .net 7 the problem starts occurring: I have MediaPicker placed to one xaml page of my project in order to capture a picture and store it at the app data directory. I can run Android emulator and get permission for camera, but it stopped working for storage. I deleted and re-created Android emulator device, I tried different Android versions like API 29, 33, but nothing works. I used to pop up Android permission dialog window automatically when it executes CapturePhotoAsync, now it does not, and even when explicitly request the permission it still does not show dialog window and retuns Denied immidiatelly

// got PermissionStatus.Denied, no dialog window requesting permission pops up
PermissionStatus status = await Permissions.RequestAsync<Permissions.StorageWrite>();

If I ignore it and tries to store the picture it predictably raises the exception Microsoft.Maui.ApplicationModel.PermissionException: 'StorageWrite permission was not granted: Denied'

Anyone can suggest what could be wrong and what can I try to get it resolved

UPDATE: created min. project and uploaded it to the github: github.com/YMichurin/mauiStoragePermissions

CodePudding user response:

I have done a sample to test with your code. And the permission request dialog will show after I clicked the button.

Did you forget to add the permission into the AndroidManifest.xml? Such as:

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

In addition, you can create a new project with the .net 7.0 to test the code. It worked well in my project.

Update

When I cloned your project to the vs, there was some packages errors. And the project run successfully after I added the following code into the csproj.cs.

<ItemGroup>
   <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0-rc.2.22430.11" />
   <PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0-rc.2.22430.11" />
   <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0-rc.2.22430.11" />
   <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0-rc.2.22430.11" />
</ItemGroup>

And the Result:

enter image description here

The console log:

enter image description here

The permission request dialog will show and the picture saved successfully after I granted the permission.

  • Related