0
在我的應用程序中,我有一個按鈕,拍攝照片並保存。預覽不顯示。快速拍照時應用程序崩潰
該應用程序工作正常,如果我點擊按鈕每1-2s,但如果我點擊它更快的應用程序崩潰。只要不點擊按鈕,我就可以拍攝所需的所有照片。
PhotoFragment.java:
public class PhotoFragment extends Fragment {
private final static String LOG_TAG = PhotoFragment.class.getSimpleName();
private Camera mCamera;
private SurfaceTexture mSurfaceTexture; // mSurfaceTexture must be global to avoid log error spam
public static PhotoFragment newInstance() {
Bundle args = new Bundle();
PhotoFragment fragment = new PhotoFragment();
fragment.setArguments(args);
return fragment;
}
public PhotoFragment(){}
@Override
public void onStart() {
super.onStart();
getActivity().setTitle(R.string.photo);
}
@Override
public void onResume() {
super.onResume();
safeCameraOpen();
}
@Override
public void onPause() {
super.onPause();
releaseCamera();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_content, container, false);
Button yo = (Button) rootView.findViewById(R.id.pic);
yo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mCamera == null){
Log.w(LOG_TAG, "@onCreateView mCamera is null!");
}
mCamera.takePicture(null, null, mCall);
}
});
Button yoyo = (Button) rootView.findViewById(R.id.vid);
yoyo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return rootView;
}
Camera.PictureCallback mCall = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//decode the data obtained by the camera into a Bitmap and save it
FileOutputStream outStream = null;
try {
String sdCardDirectory = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.getPath();
outStream = new FileOutputStream(sdCardDirectory
+ Utils.getPictureFilename());
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
mCamera.startPreview();
}
};
private boolean safeCameraOpen() {
boolean qOpened = false;
try {
releaseCamera();
mCamera = Camera.open();
mSurfaceTexture = new SurfaceTexture(Activity.MODE_PRIVATE);
mCamera.setPreviewTexture(mSurfaceTexture);
mCamera.startPreview();
qOpened = (mCamera != null);
} catch (Exception e) {
Log.e(LOG_TAG, "failed to open Camera");
e.printStackTrace();
}
return qOpened;
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
}
堆棧跟蹤:在您的onClick
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dcs.hiddencamera, PID: 10561
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1434)
at android.hardware.Camera.takePicture(Camera.java:1379)
at com.dcs.hiddencamera.PhotoFragment$1.onClick(PhotoFragment.java:72)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
感謝您的回覆,您如何建議啓用/禁用按鈕?也許是一個全局變量? – Ddddddd
耶和去與一個實例變量的按鈕。 –
我測試了它並修復了它。謝謝。這非常簡單。 – Ddddddd