在用于相机的应用程序中,我使用了 Fotoapparat版本2.4.0 库。该应用程序正在 Android 4.3 平台上进行测试 问题是第一次拍摄后,相机会停止拍照。
代码:
public class NewPostFragment extends Fragment implements View.OnClickListener {
private CameraView mCameraView;
private RecyclerView mPhotosRecyclerView;
private PhotoRibbonAdapter mPhotoRibbonAdapter;
private ImageButton mTakePhotoButton;
private Toolbar toolbar;
private Fotoapparat mFotoapparat;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new_post, container, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
mPhotosRecyclerView = view.findViewById(R.id.preview_photos);
mCameraView = view.findViewById(R.id.camera_view);
mTakePhotoButton = view.findViewById(R.id.take_photo_button);
toolbar = view.findViewById(R.id.camera_toolbar);
mTakePhotoButton.setOnClickListener(this);
mPhotosRecyclerView.setLayoutManager(layoutManager);
mPhotoRibbonAdapter = new PhotoRibbonAdapter();
mPhotosRecyclerView.setAdapter(mPhotoRibbonAdapter);
mFotoapparat = Fotoapparat
.with(getContext())
.into(mCameraView)
.build();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onStart() {
super.onStart();
mFotoapparat.start();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.take_photo_button :
PhotoResult photoResult = mFotoapparat.takePicture();
photoResult.toBitmap().whenDone(new WhenDoneListener<BitmapPhoto>() {
@Override
public void whenDone(@org.jetbrains.annotations.Nullable BitmapPhoto bitmapPhoto) {
//The second time, the bitmapPhoto parameter is null!
if (bitmapPhoto != null) {
mPhotoRibbonAdapter.add(bitmapPhoto.bitmap);
mPhotoRibbonAdapter.notifyDataSetChanged();
}
}
});
break;
}
}
此外,我注意到在拍摄第一张照片后,相机和整个应用程序开始变慢。