Xamarin.Forms 拍照 Plugin.Media 无效

Xamarin.Forms Taking picture with Plugin.Media not working

我正在使用来自@JamesMontemagno 2.4.0-beta 版的 Plugin.Media(它修复了图片方向),它在 Adroind 4.1.2 (Jelly Bean) 和 Marshmallow 上运行,但不适用于我的 Galaxy Android 版本 5.1.1.

的 S5 Neo

基本上,当我拍照时,它永远不会 returns 回到我开始该过程的页面;总是returns回到初始主页。

在它工作的设备上,当我拍照时,我看到应用程序首先触发 OnSleep,然后在拍照后触发 OnResume。 在我不工作的设备上,它会触发 OnSleep,在拍照后不会触发 OnResume,它会触发初始化页面,然后触发 OnStart。 因此,它不会打开我拍照时所在的页面。

我应该怎么做才能确保它触发 OnResume 返回到正确的页面而不是 OnStart,后者 returns 在初始 fome 页面上?

此外,当我拍照时,等待 TakePhotoAsync 过程后需要将近 30 秒才能返回代码,而且速度太慢了!

按照我的代码:

MyTapGestureRecognizerEditPicture.Tapped += async (sender, e) =>           
{               
            //Display action sheet
            String MyActionResult = await DisplayActionSheet(AppLocalization.UserInterface.EditImage, 
                                                            AppLocalization.UserInterface.Cancel, 
                                                            AppLocalization.UserInterface.Delete,
                                                            AppLocalization.UserInterface.TakePhoto, 
                                                            AppLocalization.UserInterface.PickPhoto);                
            //Execute action result                               
            if (MyActionResult == AppLocalization.UserInterface.TakePhoto)
            {
                //-----------------------------------------------------------------------------------------------------------------------------------------------
                //Take photo               
                await CrossMedia.Current.Initialize();
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert(AppLocalization.UserInterface.Alert, AppLocalization.UserInterface.NoCameraAvailable, AppLocalization.UserInterface.Ok);
                }
                else
                {                        
                    var MyPhotoFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                    {
                        Directory = "MyApp",
                        Name = "MyAppProfile.jpg",
                        SaveToAlbum = true,
                        PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small
                    });                        
                    if (MyPhotoFile != null)
                    {                            
                        //Render image
                        MyProfilePicture.Source = ImageSource.FromFile(MyPhotoFile.Path);                                                        
                        //Save image on database
                        MemoryStream MyMemoryStream = new MemoryStream();
                        MyPhotoFile.GetStream().CopyTo(MyMemoryStream);
                        byte[] MyArrBytePicture = MyMemoryStream.ToArray();
                        await SaveProfilePicture(MyArrBytePicture);
                        MyPhotoFile.Dispose();
                        MyMemoryStream.Dispose();                             
                    }                        
                }
            }
            if (MyActionResult == AppLocalization.UserInterface.PickPhoto)
            {
                //-----------------------------------------------------------------------------------------------------------------------------------------------
                //Pick photo
                await CrossMedia.Current.Initialize();
                if (!CrossMedia.Current.IsPickPhotoSupported)
                {
                    await DisplayAlert(AppLocalization.UserInterface.Alert, AppLocalization.UserInterface.PermissionNotGranted, AppLocalization.UserInterface.Ok);
                }
                else
                {                        
                    var MyPhotoFile = await CrossMedia.Current.PickPhotoAsync();
                    if (MyPhotoFile != null)
                    {                            
                        //Render image
                        MyProfilePicture.Source = ImageSource.FromFile(MyPhotoFile.Path);
                        //Save image on database
                        MemoryStream MyMemoryStream = new MemoryStream();
                        MyPhotoFile.GetStream().CopyTo(MyMemoryStream);
                        byte[] MyArrBytePicture = MyMemoryStream.ToArray();
                        await SaveProfilePicture(MyArrBytePicture);
                        MyPhotoFile.Dispose();
                        MyMemoryStream.Dispose();                                                               
                    }                        
                }
            }                
        };

求助!!我们需要部署这个应用程序,但我们无法解决这个问题。 提前致谢!

让 Android OS 终止并重新启动 Activity 是完全正常的。如您所见,当相机应用程序退出并且 OS return 控制您的应用程序时,您的应用程序的 Activity 将自动重新启动。很可能只是需要更多内存才能使用 Neo 的 16MP 相机拍摄该照片,您可以观看 logcat 输出以确认这一点。

Restarted – It is possible for an activity that is anywhere from paused to stopped in the lifecycle to be removed from memory by Android. If the user navigates back to the activity it must be restarted, restored to its previously saved state, and then displayed to the user.

要做什么:

因此,在 Xamarin.Forms OnStart 生命周期方法中,您需要将应用程序恢复到有效的 运行 状态(初始化变量、执行任何绑定等...)。

插件代码:

TakePhotoAsync 方法的 Android 平台 code 对我来说看起来不错,但请记住,通过 Task 传回的图像的内存将加倍,因为它从 ART VM 返回 Mono VM。在 return 之后尽快调用 GC.Collect() 会有所帮助(但是你的 Activity 正在重新启动......)

public async Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options)
        {
        ~~~
        var media = await TakeMediaAsync("image/*", MediaStore.ActionImageCapture, options);

依次调用:

this.context.StartActivity(CreateMediaIntent(id, type, action, options));

您可以在 Android OS 中真正做到弹出相机。

In addition, when I take a picture it takes almost 30 seconds to get back to the code after awaiting TakePhotoAsync process, and it's too slow!

你的 Neo 上有吗?还是所有设备?

我认为这非常可疑(即错误),因为甚至在本机相机 Intent/Activity 和您的应用程序的重启时间之后刷新所有 Java 内存Activity 在八核 1.6 GHz Cortex 上应该不会花 30 秒......但我面前没有你的设备、应用程序和代码....