android - 多次调用 onCreate 中启动的 Intent

标签 android android-intent android-camera android-lifecycle oncreate

我正在制作一个使用相机的应用程序,我希望在打开应用程序后立即打开默认相机。我目前在主要 Activity 的 onCreate 方法中开始我的图像捕获 Intent 。这有时工作得很好,但有时相机 Intent 会连续启动 3 次。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImageView = (ImageView) findViewById(R.id.imageView1);
        mImageBitmap = null;

        Button picBtn = (Button) findViewById(R.id.pictureButton);
        setBtnListenerOrDisable(
                picBtn,
                mTakePicOnClickListener,
                MediaStore.ACTION_IMAGE_CAPTURE
        );

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
        } else {
            mAlbumStorageDirFactory = new BaseAlbumDirFactory();
        }
        dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
    }

private void dispatchTakePictureIntent(int actionCode) {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f;
        try {
            f = setUpPhotoFile();
            mCurrentPhotoPath = f.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        } catch (IOException e) {
            e.printStackTrace();
            mCurrentPhotoPath = null;
        }

        startActivityForResult(takePictureIntent, actionCode);
    }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            handleBigCameraPhoto();
        }
    }

private void handleBigCameraPhoto() {

        if (mCurrentPhotoPath != null) {
            setPic();
            galleryAddPic();
            lastPhotoPath = mCurrentPhotoPath;
            mCurrentPhotoPath = null;
        }

    }

 private void setPic() {

        /* There isn't enough memory to open up more than a couple camera photos */
        /* So pre-scale the target bitmap into which the file is decoded */

        /* Get the size of the ImageView */
        int targetW = mImageView.getWidth();
        int targetH = mImageView.getHeight();

        /* Get the size of the image */
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        /* Figure out which way needs to be reduced less */
        int scaleFactor = 1;
        if ((targetW > 0) || (targetH > 0)) {
            scaleFactor = Math.min(photoW / targetW, photoH / targetH);
        }

        /* Set bitmap options to scale the image decode target */
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        /* Decode the JPEG file into a Bitmap */
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

        /* Associate the Bitmap to the ImageView */
        mImageView.setImageBitmap(bitmap);
        mImageView.setVisibility(View.VISIBLE);
    }

最佳答案

我认为 Nathaniel 给了你很好的建议,将你的相机 Intent 启动转移到 onResume 中。

但是,您需要区分 onResume 是您的 Activity 第一次开始,还是因为您的 Activity 在相机 Intent 完成后恢复而正在发生。如果你不这样做,你会得到你看到的循环。

为此,您可以更改 onActivityResult()在您的 Activity 中设置一个名为 isResumingFromCaptureIntent 之类的成员变量。当 resultCode 与您用于启动相机 Intent 的内容匹配时,在 onActivityResult 中将其设置为 true。然后,在您的 onResume 中,检查 isResumingFromCaptureIntent,如果为真,则您知道您不需要启动相机 Intent 并且可以将其设置回 false 并继续执行您的 Activity 需要执行的任何其他操作。

关于android - 多次调用 onCreate 中启动的 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682892/

相关文章:

android - 将 Camera2 API 与 ImageReader 结合使用

android - 为什么我无法在 Android Instant App 中访问相机?

java - "Failure delivering result",由于尝试图像捕获时 Motorola Bravo 上的 ManagedQuery 中出现 NPE。在 Evo 上运行良好

java - 将 Firebase 字符串数组检索到数组

android - 如何在 Android UI 线程异步中执行一些代码?

android - Intent 播放原始文件夹中的声音

android - 使用Kotlin将Image从ImageView传递到Android中的另一个Activity

java - 从 fragment 访问主要 Activity View

android - paddingStart/Left 和 paddingEnd/Right

android - 如何检查 putExtra 是否存在?