我使用 xamarin camera2 api。 我的项目以相同的名称保存照片(覆盖一张图像)。 我为此设置了新文件名,但它会覆盖第一张图片。
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;
}
第一次:我拍了一张照片,它的工作并显示消息:保存的图像path/pic.jpg
第二次:当我拍照时,它显示消息:已保存图像 path/pic1.jpg ,但 pic1.jpg 未创建并且图片保存在 pic.jpg
我在 getOutputMediaFile()
mFile = pic1.jpg 中使用 Visual Studio 调试,但在 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;
}