Android默认相机应用程序打开两次

时间:2011-08-05 21:18:22

标签: android android-intent camera

我在Android上启动默认的相机应用程序,使用以下代码在我的应用程序中获取图片:

//create parameters for Intent with filename
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");

        //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
        imageUri = getContentResolver().insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        //create new Intent
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        startActivityForResult(intent, actionCode);

并在onActivityResult方法中捕捉图片。

通常,这样可以正常工作,设备会拍照并将其返回到应用程序,但有时在完成相机应用程序后(通过保存图像或点击取消),它将第二次启动相机应用程序。如何阻止应用程序打开两次?

编辑:感谢Krylez的评论,我能够提出解决方案。

我已经使用静态类来保存来自相机的图像,以便我可以通过选项卡访问它来访问,所以我也在其中放置了一个布尔值。现在,在我开始处理相机的Activity之前,我将布尔值设置为true,然后在检查之后将其设置为false,这样如果再次调用onCreate方法,它将不再加载相机。

2 个答案:

答案 0 :(得分:2)

感谢Krylez的评论,我能够提出解决方案。

我已经使用静态类来保存来自相机的图像,以便我可以通过选项卡访问它来访问,所以我也在其中放置了一个布尔值。现在,在我开始处理相机的Activity之前,我将布尔值设置为true,然后在检查之后将其设置为false,这样如果再次调用onCreate方法,它将不再加载相机。

答案 1 :(得分:0)

我能够通过使用相同的布尔技术但通过共享首选项,在首选项中存储yes或no以及围绕新意图来解决此问题。

              String val=sharedPref.getString(...);
              if(val.equals("true"))
                { launch new intent
                  sharedPrefEditor.putstring("..","false");
                  sharedPrefEditor.commit();
                 }

它解决了问题,相机不会运行两次。 感谢。