2

Getting

below error:-

make sure class name exists is public and has an empty constructor that is public android

when calling

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_CODE);

I want example for galaxy devices for camera intent please any body can help regarding samsung device issue!

Note:- it is may be recreating the activity and finaly sometimes crashes app either image not fetching so please help me!

Stacktrace:-

java.lang.RuntimeException: Unable to start activity ComponentInfo{MYActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment MYFragment: make sure class name exists, is public, and has an empty constructor that is public at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

Hardy
  • 2,576
  • 1
  • 23
  • 45

2 Answers2

2

Found answer on my own here is the best example which is it self bothering about the device!

AndroidCameraUtils - Download the project and from library project by including it below is the code snippet you can use !

 private void setupCameraIntentHelper() {
    mCameraIntentHelper = new CameraIntentHelper(this, new CameraIntentHelperCallback() {
        @Override
        public void onPhotoUriFound(Date dateCameraIntentStarted, Uri photoUri, int rotateXDegrees) {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_found) + photoUri.toString());

            Bitmap photo = BitmapHelper.readBitmap(CameraIntentActivity.this, photoUri);
            if (photo != null) {
                photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees);
                ImageView imageView = (ImageView) findViewById(de.ecotastic.android.camerautil.sample.R.id.activity_camera_intent_image_view);
                imageView.setImageBitmap(photo);
            }
        }

        @Override
        public void deletePhotoWithUri(Uri photoUri) {
            BitmapHelper.deleteImageWithUriIfExists(photoUri, CameraIntentActivity.this);
        }

        @Override
        public void onSdCardNotMounted() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sd_card_not_mounted), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCanceled() {
            Toast.makeText(getApplicationContext(), getString(R.string.warning_camera_intent_canceled), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCouldNotTakePhoto() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_could_not_take_photo), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onPhotoUriNotFound() {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_not_found));
        }

        @Override
        public void logException(Exception e) {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sth_went_wrong), Toast.LENGTH_LONG).show();
            Log.d(getClass().getName(), e.getMessage());
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    mCameraIntentHelper.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mCameraIntentHelper.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    mCameraIntentHelper.onActivityResult(requestCode, resultCode, intent);
}
}
  • Add below lined to manifest file of your activityandroid:configChanges="keyboardHidden|orientation|screenSize"

NOTE:- I tried many examples for camera utils and ofcourse there are another ways to handle it but for beginners and person who are not too much familier with the core concepts would be more comfort with this project. THanks!

Hardy
  • 2,576
  • 1
  • 23
  • 45
0

Somewhere, you have a fragment. That fragment cannot be instantiated by the framework as part of recreating your activity. Either:

  • Your fragment is an inner class (i.e., not a regular Java class or a static nested class), or

  • Your fragment is not public, or

  • Your fragment does not have a zero-argument public constructor

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes but with other devices it is working fine nd i dnt want to do too much hard code for this part so is there any easy way for it? – Hardy Dec 05 '15 at 12:47
  • @HBdroid: "but with other devices it is working fine" -- no, it is not. You will run into this same crash on a configuration change (screen rotation, locale change, etc.). In this case, you are running into it because Android terminated your process while it was in the background, to free up system RAM, but the user returned to the outstanding task. This will happen with a *lot* of camera apps that you have not yet tried, and other situations besides (user switches to another app for 15 minutes, then comes back to your app). – CommonsWare Dec 05 '15 at 12:51
  • @HBdroid: With regards to the fix, not only is this the easy way, it is the only way. – CommonsWare Dec 05 '15 at 12:52
  • yes agree with you totally but how to resolve it? thanks for such a great explanation but i want the simple and easy solution for it thanks – Hardy Dec 05 '15 at 12:53