1

Suppose an app starts, and it is unable to detect the device camera.

Then I want to show not find camera!! dialog

I think to use UnhandledExceptionHandler in onCreate

Is it correct or something else would be better ?

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
chohyunwook
  • 411
  • 1
  • 5
  • 18

2 Answers2

0

You can wrap Camera.open() in try … catch. Some other camera APIs, including setParameters(), can throw RuntimeException, too.

It's not a good idea to wrap all of your Activity's onCreate() in one huge try … catch: you want the activity to get correctly created even if the camera fails, at least so that it can host the dialog.

By the way, the good practice is not to call Camera.open() from onCreate(), but rather use a background Handler thread.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

Alternatively you can also do it like this :

public class MainActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }


}

private boolean isDeviceSupportCamera() {
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

 }
Lips_coder
  • 686
  • 1
  • 5
  • 17