我正在使用@JamesMontemagno 2.4.0-beta版本(用于修复图片方向)中的Plugin.Media,它可以在Adroind 4.1.2(Jelly Bean)和棉花糖上使用,但不能在装有Android 5.1的Galaxy S5 Neo上使用。 1。
基本上,当我拍照时,它永远不会返回到我开始该过程的页面;总是返回到初始主页。
在可以工作的设备上,当我拍照时,我首先看到应用程序触发OnSleep,然后在拍照后触发OnResume。在无法正常工作的设备上,它将触发OnSleep,并且在拍照后不触发OnResume,它将触发初始化页面,然后触发OnStart。因此,它无法打开我拍照时的页面。
我应该怎么做才能确保它触发OnResume返回正确的页面,而不触发OnStart并返回初始页面?
另外,当我拍照时,在等待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
。如您所见,Activity
当照相机应用程序退出并且操作系统将控制权返回给您的应用程序时,您的应用程序将自动重新启动。很有可能只是为了使用Neo的16MP相机拍摄照片而需要更多的存储空间,您可以观看logcat
输出以确认这一点。
重新启动– Android可能会将生命周期中从暂停到停止的任何活动从内存中删除。如果用户导航回到该活动,则必须重新启动该活动,将其还原到先前保存的状态,然后向用户显示。
因此,在Xamarin.FormsOnStart
生命周期方法上,您需要将应用程序还原到有效的运行状态(初始化变量,执行任何绑定等)。
该方法的Android平台代码对TakePhotoAsync
我来说看起来不错,但请记住,通过传回该图像的内存Task
将翻倍,因为它是从ART VM整理回Mono VM的。GC.Collect()
退货后尽快致电会有所帮助(但您的“活动”仍在重新启动...)
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中真正完成弹出照相机的操作。
另外,当我拍照时,在等待TakePhotoAsync过程之后,几乎需要30秒钟才能返回到代码,而且太慢了!
那是在您的Neo上吗?还是所有设备?
我会称其为非常可疑的(即错误),因为即使在本机Camera Intent
/之后刷新所有Java内存,Activity
并且在Activity
八核1.6 GHz皮质上,应用程序的重启时间也不需要30秒...但是我愿意我面前没有您的设备,应用和代码。...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句