屏幕锁定后相机冻结

Camera freezes after screen lock

我正在为 Android 使用 zbar 开发条码扫描器。 一切正常。但是,当我的应用程序进入休眠状态或我的 phone 锁定时,相机预览会冻结,我无法在恢复时重新启动它。

fragment.java

package com.alsafeer.xpressions.fragment;

import android.graphics.Typeface;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alsafeer.xpressions.AnalyticsSampleApp;
import com.alsafeer.xpressions.CameraPreview;
import com.alsafeer.xpressions.JsonParser;
import com.alsafeer.xpressions.R;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class rewards extends Fragment {
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
private String Name;
private String Email;
private View view;
private String Mob;
private String Points;
private String result;
private RelativeLayout preview;

TextView scanText;
Button scanButton;

ImageScanner scanner;

private boolean barcodeScanned = false;
private boolean previewing = true;

static {
    System.loadLibrary("iconv");
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    view = inflater.inflate(R.layout.activity_rewards,
            container, false);
    view.setFocusableInTouchMode(true);
    view.requestFocus();
    Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
            AnalyticsSampleApp.TrackerName.APP_TRACKER);
    GoogleAnalytics.getInstance(getActivity()).setLocalDispatchPeriod(1);
    t.setScreenName("Rewards Barcode Reader");
    t.send(new HitBuilders.AppViewBuilder().build());
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/font.ttf");
    Button next= (Button) view.findViewById(R.id.next);
    next.setTypeface(font);
    scanText = (EditText)view.findViewById(R.id.editText6);
    scanText.setTypeface(font);
    mCamera = getCameraInstance();
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!scanText.getText().toString().equals(""))
            {
                new AsyncTaskParseJson().execute();
            }
            else
            {
                Toast.makeText(getActivity(),"Please enter the number or scan the barcode...", Toast.LENGTH_LONG).show();
            }
        }
    });
        scanText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                        releaseCamera();
                        getFragmentManager().beginTransaction().replace(R.id.content_frame, new FirstFragment(), "first").commit();
                        return true;
                    }

                }
                return false;
            }
        });
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                    releaseCamera();
                    getFragmentManager().beginTransaction().replace(R.id.content_frame, new FirstFragment(), "first").commit();
                    return true;
                }
            }
            return true;
        }
    });

    autoFocusHandler = new Handler();


    /* Instance barcode scanner */
    scanner = new ImageScanner();
    scanner.setConfig(0, Config.X_DENSITY, 3);
    scanner.setConfig(0, Config.Y_DENSITY, 3);

    mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);
    preview = (RelativeLayout) view.findViewById(R.id.cameraPreview);
    preview.addView(mPreview);
    scanButton = (Button)view.findViewById(R.id.button6);
    scanButton.setTypeface(font);
    scanButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (barcodeScanned) {
                barcodeScanned = false;
                mCamera.setPreviewCallback(previewCb);
                mCamera.startPreview();
                previewing = true;
                mCamera.autoFocus(autoFocusCB);
            }
        }
    });
    return view;
}

@Override
public void onResume() {
    super.onResume();
    if(mCamera==null) {
        mCamera=getCameraInstance();
        mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);
        preview = (RelativeLayout) view.findViewById(R.id.cameraPreview);
        preview.addView(mPreview);
        mCamera.setPreviewCallback(previewCb);
        mCamera.startPreview();
        previewing = true;
        mCamera.autoFocus(autoFocusCB);
    }

}

@Override
public void onPause() {
    super.onPause();

}

@Override
public void onStop() {
    super.onStop();
    releaseCamera();
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e){
    }
    return c;
}

public void releaseCamera() {
    if (mCamera != null) {
        previewing = false;
        mCamera.setPreviewCallback(null);
        mCamera.release();
        mCamera = null;
    }
}

private Runnable doAutoFocus = new Runnable() {
    public void run() {
        if (previewing)
            mCamera.autoFocus(autoFocusCB);
    }
};

Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = parameters.getPreviewSize();

        Image barcode = new Image(size.width, size.height, "Y800");
        barcode.setData(data);

        int result = scanner.scanImage(barcode);

        if (result != 0) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();

            SymbolSet syms = scanner.getResults();
            for (Symbol sym : syms) {
                scanText.setText(sym.getData());
                barcodeScanned = true;
            }
        }
    }
};

// Mimic continuous auto-focusing
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
    public void onAutoFocus(boolean success, Camera camera) {
        autoFocusHandler.postDelayed(doAutoFocus, 1000);
    }
};
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
    JSONArray dataJsonArr = null;

    @Override
    protected void onPreExecute() {}

    @Override
    protected String doInBackground(String... arg0) {

        try {

            // instantiate our json parser
            JsonParser jParser = new JsonParser();

            // get json string from url
            JSONObject json = jParser.getJSONFromUrl("http://www.safeerplus.ae/ncno="+scanText.getText().toString());

            // get the array of users
            dataJsonArr = json.getJSONArray("msg");

            // loop through all users

            JSONObject c = dataJsonArr.getJSONObject(0);

            // Storing each json item in variable
            Name = c.getString("name");
            Mob = c.getString("mob");
            Email = c.getString("email");
            Points = c.getString("points");
            if(Name.equals("")||Mob.equals("")||Email.equals("")||Points.equals(""))
            {

            }
            else {
                Bundle args = new Bundle();
                args.putString("code", scanText.getText().toString());
                args.putString("name1", Name);
                args.putString("mob1", Mob);
                args.putString("email1", Email);
                args.putString("points1", Points);
                releaseCamera();
                Fragment fr=new reward_details();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                fr.setArguments(args);
                ft.replace(R.id.content_frame, fr);
                ft.commit();
            }



        } catch (JSONException e) {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {

                    Toast.makeText(getActivity(), "Please use a valid card!", Toast.LENGTH_LONG).show();
                }
            });

        }
      return null;
    }


    @Override
    protected void onPostExecute(String strFromDoInBg) {}
}
}

camerapreview.java

package com.alsafeer.xpressions;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;

public CameraPreview(Context context, Camera camera,
                     PreviewCallback previewCb,
                     AutoFocusCallback autoFocusCb) {
    super(context);
    mCamera = camera;
    previewCallback = previewCb;
    autoFocusCallback = autoFocusCb;

    /* 
     * Set camera to continuous focus if supported, otherwise use
     * software auto-focus. Only works for API level >=9.
     */
    /*
    Camera.Parameters parameters = camera.getParameters();
    for (String f : parameters.getSupportedFocusModes()) {
        if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
            mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            autoFocusCallback = null;
            break;
        }
    }
    */

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);

    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        // open the camera
        mCamera = Camera.open();
    } catch (RuntimeException e) {
        // check for exceptions
        System.err.println(e);
        return;
    }
    Camera.Parameters param;
    param = mCamera.getParameters();

    // modify parameter
    param.setPreviewSize(200, 300);
    mCamera.setParameters(param);
    try {
        // The Surface has been created, now tell the camera where to draw
        // the preview.
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (Exception e) {
        // check for exceptions
        System.err.println(e);
        return;
    }
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}

public void surfaceDestroyed(SurfaceHolder holder) {

}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters parameters = mCamera.getParameters();
    final DisplayMetrics dm = this.getResources().getDisplayMetrics();
    //getBestPreviewSize returns the best preview size, don't worry
    //but some devices do not return one for all camera matching the aspect ratio

    /*
     * If your preview can change or rotate, take care of those events here.
     * Make sure to stop the preview before resizing or reformatting it.
     */
    if (mHolder.getSurface() == null){
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e){
      // ignore: tried to stop a non-existent preview
    }

    try {
        // Hard code camera surface rotation 90 degs to match Activity view in portrait
        mCamera.setDisplayOrientation(90);

        mCamera.setPreviewDisplay(mHolder);
        mCamera.setPreviewCallback(previewCallback);
        mCamera.startPreview();
        mCamera.autoFocus(autoFocusCallback);
    } catch (Exception e){
        Log.d("DBG", "Error starting camera preview: " + e.getMessage());
    }
}
}

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#e7e7e9">

<RelativeLayout
    android:id="@+id/cameraPreview"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_weight="1"
    android:gravity="clip_horizontal"
    android:layout_below="@+id/editText6"
    android:layout_centerHorizontal="true">

</RelativeLayout>

<Button
    android:id="@+id/next"
    android:text="View points"
    android:layout_width="120dp"
    android:layout_height="30sp"
    android:layout_gravity="center"
    android:background="@drawable/loginbutton"
    android:textSize="12sp"
    android:layout_alignTop="@+id/button6"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:id="@+id/button6"
    android:text="Rescan"
    android:layout_width="120dp"
    android:layout_height="30sp"
    android:layout_gravity="center"
    android:background="@drawable/loginbutton"
    android:textSize="12sp"
    android:layout_below="@+id/cameraPreview"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText6"
    android:background="#d1d2d4"
    android:textSize="15sp"
    android:padding="5dp"
    android:textColor="#000000"
    android:hint="Enter or Scan the Barcode"
    android:textColorHint="#4a4f5a"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/cameraPreview"
    android:layout_alignStart="@+id/cameraPreview"
    android:layout_alignRight="@+id/cameraPreview"
    android:layout_alignEnd="@+id/cameraPreview" />

</RelativeLayout>

logcat

04-20 10:40:20.356    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ setHasPreviewCallback: installed:1, manualBuffer:0
04-20 10:40:20.359    7099-7099/com.alsafeer.xpressions I/CameraFramework﹕ Camera framework getParameters =afeng-max-focus-step=0;afeng-min-focus-step=0;antibanding=auto;antibanding-values=off,50hz,60hz,auto;auto-exposure-lock-supported=true;auto-whitebalance-lock-supported=true;brightness=middle;brightness-values=low,middle,high;brightness_value=39;burst-num=1;cap-mode=normal;cap-mode-values=normal,face_beauty,continuousshot,smileshot,bestshot,evbracketshot,autorama,mav,asd;capfname=/sdcard/DCIM/cap00;contrast=middle;contrast-values=low,middle,high;edge=middle;edge-values=low,middle,high;effect=none;effect-values=none,mono,negative,sepia,aqua,whiteboard,blackboard;eng-flash-duty-max=1;eng-flash-duty-min=0;eng-flash-duty-value=-1;exposure-compensation=0;exposure-compensation-step=1.0;fb-sharp=0;fb-sharp-max=4;fb-sharp-min=-4;fb-skin-color=0;fb-skin-color-max=4;fb-skin-color-min=-4;fb-smooth-level=0;fb-smooth-level-max=4;fb-smooth-level-min=-4;focal-length=3.5;focus-distances=0.95,1.9,Infinity;focus-mode=infinity;focus-mode-values=infinity;horizontal-view-angle=54;hue=middle;hue-values=low,middle,high;iso-speed=auto;iso-speed-values=auto,100,200,400,800,1600;jpeg-quality=100;jpeg-thumbnail-height=128;jpeg-thumbnail-quality=100;jpeg-thumbnail-size-values=0x0,160x128,320x240;jpeg-thumbnail-width=160;max-exposure-compensation=3;max-num-detected-faces-hw=15;max-num-detected-faces-sw=0;max-num-focus-areas=0;max-num-metering-areas=9;max-zoom=10;min-exposure-compensation=-3;mtk-cam-mode=0;picture-format=jpeg;picture-format-values=jpeg;picture-size=2560x1920;picture-size-values=320x240,640x480,1024x768,1280x720,1280x768,1280x960,1600x1200,2048x1536,2560x1440,2560x1920,2880x1728;preferred-preview-size-for-video=1280x720;preview-format=yuv420sp;preview-format-values=yuv420sp,yuv420p,yuv420i-yyuvyy-3plane;preview-fps-range=5000,60000;preview-fps-range-values=(5000,60000);preview-frame-rate=30;preview-frame-rate-values=15,24,30;preview-size=640x480;preview-size-values=176x144,320x240,352x288,480x320,480x368,640x480,720x480,800x480,800x600,864x480,960x540,1280x720;rotation=0;saturation=middle;saturation-values=low,middle,high;scene-mode=auto;scene-mode-values=auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,hdr;sensor-type=254;smooth-zoom-supported=true;vertical-view-angle=49;video-frame-format=yuv420p;video-size=640x480;video-size-values=176x144,480x320,640x480,864x480,1280x720,1920x1080;video-snapshot-supported=false;video-stabilization=false;video-stabilization-supported=true;whitebalance=auto;whitebalance-values=auto,incandescent,fluorescent,warm-fluorescent,daylight,cloudy-daylight,twilight,shade;zoom=0;zoom-ratios=100,114,132,151,174,200,229,263,303,348,400;zoom-supported=true;zsd-mode=off;zsd-mode-values=off,on
04-20 10:40:20.390    7099-7112/com.alsafeer.xpressions D/Camera-JNI﹕ Allocating callback buffer
04-20 10:40:20.418    7099-7099/com.alsafeer.xpressions I/CameraFramework﹕ handleMessage: 16
04-20 10:40:20.418    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ setHasPreviewCallback: installed:1, manualBuffer:0
04-20 10:40:20.420    7099-7099/com.alsafeer.xpressions I/CameraFramework﹕ Camera framework getParameters =afeng-max-focus-step=0;afeng-min-focus-step=0;antibanding=auto;antibanding-values=off,50hz,60hz,auto;auto-exposure-lock-supported=true;auto-whitebalance-lock-supported=true;brightness=middle;brightness-values=low,middle,high;brightness_value=39;burst-num=1;cap-mode=normal;cap-mode-values=normal,face_beauty,continuousshot,smileshot,bestshot,evbracketshot,autorama,mav,asd;capfname=/sdcard/DCIM/cap00;contrast=middle;contrast-values=low,middle,high;edge=middle;edge-values=low,middle,high;effect=none;effect-values=none,mono,negative,sepia,aqua,whiteboard,blackboard;eng-flash-duty-max=1;eng-flash-duty-min=0;eng-flash-duty-value=-1;exposure-compensation=0;exposure-compensation-step=1.0;fb-sharp=0;fb-sharp-max=4;fb-sharp-min=-4;fb-skin-color=0;fb-skin-color-max=4;fb-skin-color-min=-4;fb-smooth-level=0;fb-smooth-level-max=4;fb-smooth-level-min=-4;focal-length=3.5;focus-distances=0.95,1.9,Infinity;focus-mode=infinity;focus-mode-values=infinity;horizontal-view-angle=54;hue=middle;hue-values=low,middle,high;iso-speed=auto;iso-speed-values=auto,100,200,400,800,1600;jpeg-quality=100;jpeg-thumbnail-height=128;jpeg-thumbnail-quality=100;jpeg-thumbnail-size-values=0x0,160x128,320x240;jpeg-thumbnail-width=160;max-exposure-compensation=3;max-num-detected-faces-hw=15;max-num-detected-faces-sw=0;max-num-focus-areas=0;max-num-metering-areas=9;max-zoom=10;min-exposure-compensation=-3;mtk-cam-mode=0;picture-format=jpeg;picture-format-values=jpeg;picture-size=2560x1920;picture-size-values=320x240,640x480,1024x768,1280x720,1280x768,1280x960,1600x1200,2048x1536,2560x1440,2560x1920,2880x1728;preferred-preview-size-for-video=1280x720;preview-format=yuv420sp;preview-format-values=yuv420sp,yuv420p,yuv420i-yyuvyy-3plane;preview-fps-range=5000,60000;preview-fps-range-values=(5000,60000);preview-frame-rate=30;preview-frame-rate-values=15,24,30;preview-size=640x480;preview-size-values=176x144,320x240,352x288,480x320,480x368,640x480,720x480,800x480,800x600,864x480,960x540,1280x720;rotation=0;saturation=middle;saturation-values=low,middle,high;scene-mode=auto;scene-mode-values=auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,hdr;sensor-type=254;smooth-zoom-supported=true;vertical-view-angle=49;video-frame-format=yuv420p;video-size=640x480;video-size-values=176x144,480x320,640x480,864x480,1280x720,1920x1080;video-snapshot-supported=false;video-stabilization=false;video-stabilization-supported=true;whitebalance=auto;whitebalance-values=auto,incandescent,fluorescent,warm-fluorescent,daylight,cloudy-daylight,twilight,shade;zoom=0;zoom-ratios=100,114,132,151,174,200,229,263,303,348,400;zoom-supported=true;zsd-mode=off;zsd-mode-values=off,on
04-20 10:40:20.457    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ setHasPreviewCallback: installed:0, manualBuffer:0
04-20 10:40:20.458    7099-7112/com.alsafeer.xpressions W/Camera-JNI﹕ callback on dead camera object
04-20 10:40:20.458    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ (tid:7099)[native_release] + context=0x66e3fc70 camera=0x63f70e30
04-20 10:40:20.458    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ [native_release] context->getStrongCount(2) camera->getStrongCount(2)
04-20 10:40:21.023    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ (tid:7099)[native_release] - context=0x66e3fc70 camera=0x0
04-20 10:40:21.023    7099-7099/com.alsafeer.xpressions D/Camera-JNI﹕ (tid:7099)[release camera] - X context=0x66e3fc70
04-20 10:40:21.024    7099-7111/com.alsafeer.xpressions D/IPCThreadState﹕ [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x66e3fba8
04-20 10:40:21.027    7099-7151/com.alsafeer.xpressions D/Camera-JNI﹕ (tid:7151)[~MtkJNICameraContext] this:0x66e3fc70
04-20 10:40:21.066    7099-7099/com.alsafeer.xpressions I/Choreographer﹕ Skipped 41 frames!  The application may be doing too much work on its main thread.
04-20 10:40:21.089    7099-7099/com.alsafeer.xpressions D/GraphicBuffer﹕ create handle(0x623e35c0) (w:480, h:790, f:1)
04-20 10:40:21.121    7099-7099/com.alsafeer.xpressions I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x63159d68) fps:0.09, dur:10931.96, max:10931.96, min:10931.96
04-20 10:40:21.121    7099-7099/com.alsafeer.xpressions I/SurfaceTextureClient﹕ [STC::queueBuffer] this:0x63159d68, api:1, last queue time elapsed:10931.96
04-20 10:40:21.130    7099-7099/com.alsafeer.xpressions D/OpenGLRenderer﹕ Flushing caches (mode 0)

似乎每次应用程序恢复 (onResume) 时,您都在重新创建 CameraPreview 视图并将其添加到布局中。当您在您的应用程序中切换到另一个应用程序然后返回时,是否也会出现此问题?如果它在这种情况下也冻结,我怀疑您需要在添加新视图实例之前从布局中删除旧的 CameraPreview 视图实例。