toda vez que eu clico no botão tirar foto aparece a mensagem de erro
Java.Lang.NullPointerException
[assembly:Xamarin.Forms.Dependency(typeof(MainActivity))]
namespace TesteDrive.Droid
{
[Activity(Label = "TesteDrive", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
,ICamera
{//----------------------------------------------------------
static Java.IO.File arquivoImagem;
public void TirarFoto()
{
var intent = new Intent(MediaStore.ActionImageCapture);
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(arquivoImagem));
var activity = Forms.Context as Activity;
activity.StartActivityForResult(intent, 0);
}
private static Java.IO.File PegarArquivoImagem()
{
Java.IO.File arquivoImagem;
Java.IO.File diretorio = new Java.IO.File(
Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryPictures), "Images");
arquivoImagem = PegarArquivoImagem();
if (!diretorio.Exists())
diretorio.Mkdirs();
arquivoImagem = new Java.IO.File(diretorio, "MinhaFoto.jpg");
return arquivoImagem;
}
//-------------------------------------------------------
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
//--------------------------------------------------------------------------------------------
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if(resultCode == Result.Ok)
{
byte[] bytes;
using(var stream = new Java.IO.FileInputStream(arquivoImagem))
{
bytes = new byte[arquivoImagem.Length()];
stream.Read(bytes);
}
MessagingCenter.Send<byte[]>(bytes, "FotoTirada");
}
}
}
}
namespace TesteDrive.ViewModels
{
public class MasterViewModel : BaseViewModel
{
//-----------------------------------------------
public ImageSource fotoPerfil ="Perfil.png";
public ImageSource FotoPerfil
{
get { return fotoPerfil; }
private set{ fotoPerfil = value; OnPropertyChanged(); }
}
//----------------------------------------------
private readonly Usuario usuario;
public ICommand EditarPerfilCommand { get; private set; }
public ICommand SalvarCommand { get; private set; }
public ICommand EditarCommand { get; private set; }
public ICommand TirarFotoCommand { get; private set; }
MessagingCenter.Subscribe<byte[]>(this,"FotoTirada",
(bytes)=>
{
FotoPerfil = ImageSource.FromStream(
() => new MemoryStream(bytes));
});
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:TesteDrive.Converters"
x:Class="TesteDrive.Views.MasterView"
Title="Perfil">
<TabbedPage.Resources>
<ResourceDictionary>
<converters:NegativoConverter x:Key="negativo"></converters:NegativoConverter>
</ResourceDictionary>
</TabbedPage.Resources>
<TabbedPage.Children>
<ContentPage Title="USUÁRIO">
<Frame OutlineColor="Silver"
Margin="15"
VerticalOptions="CenterAndExpand">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Image Source="{Binding FotoPerfil}" HeightRequest="100" WidthRequest="100"></Image>
<Label Text="{Binding Nome}"></Label>
<BoxView HeightRequest="1" HorizontalOptions="Fill" Color="Gray"></BoxView>
<Label Text="{Binding Email}"></Label>
<BoxView HeightRequest="1" HorizontalOptions="Fill" Color="Gray"></BoxView>
<Button Text="PERFIL" Command="{Binding EditarPerfilCommand}"></Button>
</StackLayout>
</Frame>
</ContentPage>
<ContentPage Title="EDITAR">
<Frame OutlineColor="Silver"
Margin="15"
VerticalOptions="CenterAndExpand">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Image Source="{Binding FotoPerfil}" HeightRequest="100" WidthRequest="100"></Image>
<Button Text="Tirar Foto" Command="{Binding TirarFotoCommand}"></Button>
<TableView>
<TableRoot>
<TableSection Title="Dados Pessoais">
<EntryCell Placeholder="Nome" Text="{Binding Nome}" IsEnabled="{Binding Editando}"></EntryCell>
<EntryCell Placeholder="Data de Nascimento" Text="{Binding DataNascimento}" IsEnabled="{Binding Editando}"></EntryCell>
<EntryCell Placeholder="Telefone" Text="{Binding Telefone}" IsEnabled="{Binding Editando}"></EntryCell>
<EntryCell Placeholder="Email" Text="{Binding Email}" IsEnabled="{Binding Editando}"></EntryCell>
</TableSection>
</TableRoot>
</TableView>
<Button Text="SALVAR" Command="{Binding SalvarCommand}" IsVisible="{Binding Editando}"></Button>
<Button Text="EDITAR" Command="{Binding EditarCommand}" IsVisible="{Binding Editando, Converter={StaticResource Key=negativo}}"></Button>
</StackLayout>
</Frame>
</ContentPage>
</TabbedPage.Children>
</TabbedPage>