gpt4 book ai didi

android - 在 Android 上从图库中选择图像时出现权限问题

转载 作者:行者123 更新时间:2023-11-30 05:12:29 25 4
gpt4 key购买 nike

我开始使用 Xamarin 是为了能够创建一个同时在 iOS 和 Android 上运行的应用程序。我要做的第一件事是单击一个按钮 -> 打开图库 -> 选择图像 -> 在图像标签上显示所选图像

MainPage.xaml 当前内容为:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:HelloWorld" x:Class="HelloWorld.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<Label Text="0" x:Name="lbl_counter" HorizontalOptions="Center" VerticalOptions="StartAndExpand" FontSize="Large"/>
<Image x:Name="img_selected" Source="" />
<Button Text="Click Me" Clicked="Handle_Clicked" VerticalOptions="StartAndExpand"/>
</StackLayout>
</ContentPage>

我正在使用 Plugin.Media,所以当按钮被点击时我运行这段代码:

async void Handle_Clicked(object sender, System.EventArgs e){
lbl_counter.Text = (++counter).ToString();

await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported){
await DisplayAlert("no upload", "picking a photo is not supported", "ok");
return;
}

var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;

img_selected.Source = ImageSource.FromStream(file.GetStream);
}

经过一些(很多)努力,我设法让它在 iOS 上运行。但我无法让它在 Android 上运行。它一直提示权限。我已经关注了所有我能想到的:

有什么想法吗?


从评论中添加信息

评论 1:

我在 Xamarin 中没有获得(可见的)堆栈跟踪。如果我从错误窗口复制,我会得到这个:

Plugin.Media.Abstractions.MediaPermissionException: Storage permission(s) are required.

在输出窗口中我可以看到:

Does not have storage permission granted, requesting.
Storage permission Denied.

代码

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Plugin.Media;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;

namespace HelloWorld
{
public partial class MainPage : ContentPage
{
private int counter;
public MainPage()
{
InitializeComponent();
}

async void Handle_Clicked(object sender, System.EventArgs e)
{
lbl_counter.Text = (++counter).ToString();

/******************************/
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
{
await DisplayAlert("Need Storage", "Gunna need that Storage", "OK");
}

var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
status = results[Permission.Storage];
}

if (status == PermissionStatus.Granted)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("no upload", "picking a photo is not supported", "ok");
return;
}

var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;

img_selected.Source = ImageSource.FromStream(file.GetStream);
}
else if (status != PermissionStatus.Unknown)
{
await DisplayAlert("Storage Permission Denied", "Can not continue, try again.", "OK");
}
}
catch
{
//...
}
/******************************/

}
}
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:HelloWorld" x:Class="HelloWorld.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<Label Text="0" x:Name="lbl_counter" HorizontalOptions="Center" VerticalOptions="StartAndExpand" FontSize="Large"/>
<Image x:Name="img_selected" Source="" />
<Button Text="Click Me" Clicked="Handle_Clicked" VerticalOptions="StartAndExpand"/>
</StackLayout>
</ContentPage>

MainActivity.cs

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace HelloWorld.Droid
{
[Activity(Label = "HelloWorld", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.gedons.HelloWorld">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="HelloWorld.Android">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>

_文件路径.xml_

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>

最佳答案

Permissions problem when selecting image from gallery on Android

从 Android 6.0(API 级别 23)开始,用户在应用运行时向应用授予权限,而不是在安装应用时。你应该requesting permissions在运行时。

系统权限分为正常和危险两类:

  • 普通权限不会直接危及用户的隐私。如果您的应用在其 list 中列出了普通权限,系统会自动授予该权限。
  • 危险权限可以让应用访问用户的 secret 数据。如果您的应用在其 list 中列出了普通权限,系统会自动授予该权限。如果您列出危险权限,则用户必须明确批准您的应用。

READ_EXTERNAL_STORAGE 分为 Dangerous permissions ,因此您应该针对 API 级别 23 及更高级别手动检查此权限。


对于 Xamarin.Forms 项目,您可以使用 PermissionsPlugin在运行时在 Xamarin.Forms 中请求此权限。

这里简单的使用,在你的MainActivity中加入这段代码:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

在需要时请求许可:

try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
if (status != PermissionStatus.Granted)
{
if(await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
{
await DisplayAlert("Need Storage", "Gunna need that Storage", "OK");
}

var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
status = results[Permission.Storage];
}

if (status == PermissionStatus.Granted)
{
//Permission granted, do what you want do.
}
else if(status != PermissionStatus.Unknown)
{
await DisplayAlert("Storage Permission Denied", "Can not continue, try again.", "OK");
}
}
catch (Exception ex)
{
//...
}

关于android - 在 Android 上从图库中选择图像时出现权限问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53490011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com