Forum Xamarin.Android

Camera Intent issue saving the picture

JosePereraJosePerera USMember ✭✭

Hello guys,

I have this issue that i would like to share with you and ask you what do you think about it:
I have an option for "SELECT" or "TAKE" an Image using the Camera APP. I will share the code that i made and everything is working fine except that in the case of take a picture using the camera i can't get the picture because it isn't saved in the Gallery, I give you the code:
imPicture.Click += (object sender, System.EventArgs e) => { if (IsThereAnAppToTakePictures ()) { List<Intent> pictureIntents = new List<Intent>(); IList<ResolveInfo> listCamera = PackageManager.QueryIntentActivities(new Intent(MediaStore.ActionImageCapture), 0); foreach (var item in listCamera) { Intent cam = new Intent(MediaStore.ActionImageCapture); cam.SetComponent(new ComponentName(item.ActivityInfo.PackageName, item.ActivityInfo.Name)); cam.SetPackage(item.ActivityInfo.PackageName); pictureIntents.Add(cam); } _file = new Java.IO.File (_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid())); Intent gal = new Intent(Intent.ActionGetContent); gal.SetType("image/*"); Intent chooseIntent = new Intent(Intent.CreateChooser(gal, "Select")); chooseIntent.PutExtra(Intent.ExtraInitialIntents, pictureIntents.ToArray()); chooseIntent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile (_file)); StartActivityForResult(chooseIntent, 0); }else{ Toast.MakeText (this, Resource.String.error_intent_camera, ToastLength.Short).Show (); } };
This is the code that i'm using to show a dialog to choose if you want to choose an image or take a picture, and this is the code that i'm using for assign the image:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult (requestCode, resultCode, data); if (resultCode == Result.Ok) { Intent mediaScanIntent = new Intent (Intent.ActionMediaScannerScanFile); Android.Net.Uri contentUri = Android.Net.Uri.FromFile (_file); mediaScanIntent.SetData (contentUri); SendBroadcast (mediaScanIntent); int height = Resources.DisplayMetrics.HeightPixels; int width = imPicture.Height ; Bitmap bitmap = _file.Path.LoadAndResizeBitmap (width, height); imPicture.SetImageBitmap(bitmap); } }
If someone have any idea that what is the problem, please let me know, i will appreciate it so much.

Thanks in advanced.

Best Answer

Answers

  • JosePereraJosePerera USMember ✭✭
    edited June 2015

    One thing that it is important, is that in the physical phone the image it is saved but when i click in ok in the camera application the camera application don't close returning the image, i change the code in the imPicture.click:
    `imPicture.Click += (object sender, System.EventArgs e) => {
    if (IsThereAnAppToTakePictures ())
    {
    List pictureIntents = new List();
    IList listCamera = PackageManager.QueryIntentActivities(new Intent(MediaStore.ActionImageCapture), 0);
    file = new Java.IO.File (_dir, String.Format("myPhoto{0}.jpg", Guid.NewGuid()));
    foreach (var item in listCamera) {
    Intent cam = new Intent(MediaStore.ActionImageCapture);
    cam.SetComponent(new ComponentName(item.ActivityInfo.PackageName, item.ActivityInfo.Name));
    cam.SetPackage(item.ActivityInfo.PackageName);
    cam.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile (_file));
    pictureIntents.Add(cam);
    }

                        Intent gal = new Intent(Intent.ActionGetContent);
                        gal.SetType("image/*");
                        Intent chooseIntent = new Intent(Intent.CreateChooser(gal, "Select"));
                        chooseIntent.PutExtra(Intent.ExtraInitialIntents, pictureIntents.ToArray());
                        StartActivityForResult(chooseIntent, 0);
                    }else{
                        Toast.MakeText (this, Resource.String.error_intent_camera, ToastLength.Short).Show ();
                    }
                };`
    
  • JosePereraJosePerera USMember ✭✭

    Thank you all, the problem was my mistake because i forget invoke the method for create the directory.
    Everything is working right.

  • JosePereraJosePerera USMember ✭✭

    Hello @Cheesebaron I have a question, finally everything work fine but there is a little problem, in the emulator work fine but i'm making some tests in a phone ZTE model Z667T con Android 4.4.2... In the phone every time that i try to take a picture the picture is save correctly but in the moment to go back the application crash. In the emulator everything is fine because i'm using the webcam as emulator camera. Is there any form that i could check any log i don't know that i could check what is the problem???

    Thanks in advance

  • CheesebaronCheesebaron DKInsider, University mod

    Look in the adb logcat. Should be visible as a window in Visual Studio, otherwise fire up a command line and simply write adb logcat.

  • JosePereraJosePerera USMember ✭✭

    Hi @Cheesebaron I'm using Xamarin Studio on Mac, the logcat is the tab in the debugger?? The problem is that when i tr to debug in the phone The application stop and i could open the application but not with the debugger, there is other way to try to figure out what is cause this in the phone and not in the emulator?

    Thanks in advance

Sign In or Register to comment.