question

hosseintavakoli-7723 avatar image
0 Votes"
hosseintavakoli-7723 asked hosseintavakoli-7723 commented

Xamarin Camera2basic does not save second picture

I'm using Xamarin camera2basic.
my project save photo on same name ( override on one image ).
I set new filename for that but it override on first picture.
here is parts of my code:

 public override void OnActivityCreated(Bundle savedInstanceState)
         {
             base.OnActivityCreated(savedInstanceState);
             mFile = getOutputMediaFile();
             mCaptureCallback = new CameraCaptureListener(this);
             mOnImageAvailableListener = new ImageAvailableListener(this, mFile);
         }
    
 public void OnClick(View v)
         {
             //button Take photo code :
             if (v.Id == Resource.Id.picture)
             {
                 TakePicture();
    
                 mFile = getOutputMediaFile();
    
             }
         }
    
    
 private File getOutputMediaFile()
         {
             File mediaStorageDir = new File(Activity.GetExternalFilesDir(null), "Pictures");
             if (!mediaStorageDir.Exists())
             {
                 if (!mediaStorageDir.Mkdirs())
                 {
                     Log.Debug("Pictures", "failed to create Dir");
                     return null;
                 }
             }
    
                
             File mediaFile= new File(mediaStorageDir.Path, "pic.jpg");
             int counter = 1;
             while (mediaFile.Exists())
             {
                 mediaFile = new File(mediaStorageDir.Path, "pic" + counter.ToString() + ".jpg");
                 counter++;
             }
    
             return mediaFile;
         }

first time : When I take a picture it's work correctly and show message : saved image path/pic.jpg
second time : When I take picture it show message : saved image path/pic1.jpg , but pic1.jpg not created and image saved on pic.jpg

I use visual studio debugging in getOutputMediaFile() mFile = pic1.jpg but in ImageAvailableListener() mFile = pic.jpg

 public ImageAvailableListener(Camera2BasicFragment fragment, File file)
         {
             if (fragment == null)
                 throw new System.ArgumentNullException("fragment");
             if (file == null)
                 throw new System.ArgumentNullException("file");
    
             owner = fragment;
             this.file = file;
         }

how ca I fix it for taking several picture?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered hosseintavakoli-7723 published

Hello,​

Welcome to our Microsoft Q&A platform!

This issue is related to generate name; When you take a photo, you should generate a different name

You should open the ImageSaver.cs's constructor, I generate a name by time temp. So you will set different name when you need take many photos, then I push Toast.

public ImageSaver(Image image, File file)
            {
                if (image == null)
                    throw new System.ArgumentNullException("image");
                if (file == null)
                    throw new System.ArgumentNullException("file");

                mImage = image;

                //generate different name everytime;
                long currentTicks = DateTime.Now.Ticks;
                DateTime dtFrom = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long currentMillis = (currentTicks - dtFrom.Ticks) / 10000;

                File m2File = new File(CameraActivity.Instance.GetExternalFilesDir(null), currentMillis+ "pic.jpg");

                CameraActivity.Instance.RunOnUiThread(new ShowToastRunnable(CameraActivity.Instance.ApplicationContext, m2File.ToString()));

                mFile = m2File;
            }


I delete owner.ShowToast("Saved: " + ImageSaver.mFile); in OnCaptureCompleted method of CameraCaptureStillPictureSessionCallback.cs.

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hosseintavakoli-7723 avatar image
0 Votes"
hosseintavakoli-7723 answered hosseintavakoli-7723 commented

Is there any way to send Filename from picture button click to ImageSaver?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Resolved by static class for file name.
tnx a lot.

0 Votes 0 ·