Drjacky / ImagePicker

📸Image Picker for Android, Pick images from Gallery or Capture a new image with Camera🖼
https://github.com/Drjacky/ImagePicker
Apache License 2.0
232 stars 57 forks source link

app crash when launch #91

Open OTO8056 opened 1 year ago

OTO8056 commented 1 year ago

App crash when click btn this is my code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        PicBtn= findViewById(R.id.picBtn);

         launcher=
                registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                        (ActivityResult result)->{
                            if(result.getResultCode()==RESULT_OK){
                                Uri uri=result.getData().getData();
                                // Use the uri to load the image
                            }else if(result.getResultCode()==ImagePicker.RESULT_ERROR){
                                // Use ImagePicker.Companion.getError(result.getData()) to show an error
                            }
                        });

        PicBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                {
                    launcher.launch(ImagePicker.Companion.with(MainActivity.this)
                            .crop()
                            .provider(ImageProvider.BOTH)
                            .cropOval()
                            .createIntent());
                }
            }
        });
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Bitmap imageBitmap = null;
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            imageBitmap = BitmapFactory.decodeFile(result.getUri().getPath());
            if (resultCode == RESULT_OK) {
                switch (LAST_REQUEST_CODE) {

                    case CAR_REQUEST:
                        saveImage(imageBitmap, new File(mCurrentPhotoPath));
                        PicBtn.setImageBitmap(imageBitmap);
                        break;

                }

            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        } else {

            startCropActivity(Uri.fromFile(new File(mCurrentPhotoPath)));
        }
    }
Drjacky commented 1 year ago

Please attach the logcat (crash) too. Also, remove all StrictMode and see if you still get the crash or not. if not, it means you have not configured the StrictMode correctly (or used it incorrectly).