android flash alert2,Turning on/off flash with Android camera2 API not

I'm creating an Android app with a custom camera and I'm switching to the new camera2 API. I have a button allowing to turn ON and OFF the flash when the back camera is on (without stopping the camera, like any classic camera app).

When I tap the flash icon, nothing happens and this is what the logcat returns:

D/ViewRootImpl: ViewPostImeInputStage processPointer 0

D/ViewRootImpl: ViewPostImeInputStage processPointer 1

I don't know why it's not working. Here is the code:

I have a RecordVideoActivity using a RecordVideoFragment. Here is the fragment's XML part that contains the flash button code:

android:id="@+id/button_flash"

android:src="@drawable/ic_flash_off"

android:layout_alignParentLeft="true"

style="@style/actions_icons_camera"

android:onClick="actionFlash"/>

And the Java code:

ImageButton flashButton;

private boolean hasFlash;

private boolean isFlashOn = false;

With in the onViewCreated:

@Override

public void onViewCreated(final View view, Bundle savedInstanceState) {

...

[some code]

...

// Flash on/off button

flashButton = (ImageButton) view.findViewById(R.id.button_flash);

// Listener for Flash on/off button

flashButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

actionFlash();

}

});

And here is the actionFlash() function definition:

private void actionFlash() {

/* First check if device is supporting flashlight or not */

hasFlash = getActivity().getApplicationContext().getPackageManager()

.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

if (!hasFlash) {

// device doesn't support flash

// Show alert message and close the application

AlertDialog alert = new AlertDialog.Builder(this.getActivity())

.create();

alert.setMessage("Sorry, your device doesn't support flash light!");

alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

});

alert.show();

return;

}

else { // the device support flash

CameraManager mCameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);

try {

String mCameraId = mCameraManager.getCameraIdList()[0];

if (mCameraId.equals("1")) { // currently on back camera

if (!isFlashOn) { // if flash light was OFF

// Turn ON flash light

try {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

mCameraManager.setTorchMode(mCameraId, true);

}

} catch (Exception e) {

e.printStackTrace();

}

// Change isFlashOn boolean value

isFlashOn = true;

// Change button icon

flashButton.setImageResource(R.drawable.ic_flash_off);

} else { // if flash light was ON

// Turn OFF flash light

try {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

mCameraManager.setTorchMode(mCameraId, false);

}

} catch (Exception e) {

e.printStackTrace();

}

// Change isFlashOn boolean value

isFlashOn = false;

// Change button icon

flashButton.setImageResource(R.drawable.ic_flash_on);

}

}

} catch (CameraAccessException e) {

Toast.makeText(getActivity(), "Cannot access the camera.", Toast.LENGTH_SHORT).show();

getActivity().finish();

}

}

}

Any idea what could be wrong?

(I already looked at this question but it doesn't address my problem)

Thank you very much for your help. This is driving me crazy.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值