2017-03-14 70 views
1

我是新來的xamarin,我想從相機拍照時,當我點擊我的主要活動按鈕,然後,一旦拍攝照片,顯示它在另一個活動的imageView中。Xamarin android - 從相機拍攝照片,然後傳遞給其他活動

你能幫我嗎?

這就是我現在所擁有的:

MainActivity:

costsButton.Click += delegate 
{ 
    Intent intent = new Intent(MediaStore.ActionImageCapture); 
    StartActivityForResult(intent, 0); 
}; 

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
{ 
    base.OnActivityResult(requestCode, resultCode, data); 
    var extra = data.GetByteArrayExtra("data"); 
    Intent intent = new Intent(this, typeof(AddFrais)); 

    intent.PutExtra("picture", extra); 
    StartActivity(intent); 
} 

AddFrais.cs:

namespace Projet_stage_2017 
{ 
    [Activity(Label = "AddFrais")] 
    public class AddFrais : Activity 
    { 
     ImageView picturefrais; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.AddFrais); 
      picturefrais = FindViewById<ImageView>(Resource.Id.ImageFrais); 
      var image = Intent.GetByteArrayExtra("picture") ?? null; 
      Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length); 

      picturefrais.SetImageBitmap(bitmap); 
     } 
    } 
} 

我不知道要放什麼東西在 「PutExtra」 在mainActivity能夠在AddFrais.cs上創建位圖...

感謝您的幫助!

+0

您收到了什麼錯誤? – apineda

+0

System.NullReferenceException:未將對象引用設置爲對象的實例。我不知道要發送給我的其他活動.. – Kurapika

回答

3

試試這個:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
{ 
    base.OnActivityResult(requestCode, resultCode, data); 

    // It's a good idea that you check this before accessing the data 
    if (requestCode == 0 && resultCode == Result.Ok) 
    { 
     //get the image bitmap from the intent extras 
     var image = (Bitmap)data.Extras.Get("data"); 

     // you might also like to check whether image is null or not 
     // if (image == null) do something 

     //convert bitmap into byte array 
     byte[] bitmapData; 
     using (var stream = new MemoryStream()) 
     { 
      image.Compress(Bitmap.CompressFormat.Png, 0, stream); 
      bitmapData = stream.ToArray(); 
     } 

     Intent intent = new Intent(this, typeof(AddFrais)); 

     intent.PutExtra("picture", bitmapData); 

     StartActivity(intent); 
    } 
    // if you got here something bad happened... 
} 

然後在你的第二個活動:

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 

    // Set our view from the "main" layout resource 
    SetContentView(Resource.Layout.AddFrais); 

    picturefrais = FindViewById<ImageView>(Resource.Id.ImageFrais); 

    //Get image from intent as ByteArray 
    var image = Intent.GetByteArrayExtra("picture"); 

    if (image != null) 
    { 
     //Convert byte array back into bitmap 
     Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length); 
     picturefrais.SetImageBitmap(bitmap); 
    } 
}  

正如你可以看到你的第二個活動代碼是最有可能是一樣的,我只是增加了一個驗證,以防止NullReferenceException異常,如果圖像不能很好地從意圖中提取出來。

希望這會有所幫助!

+1

它完美的工作!謝謝 ! – Kurapika

相關問題