ImagePicker icon indicating copy to clipboard operation
ImagePicker copied to clipboard

app crash when launch

Open OTO8056 opened this issue 2 years ago • 1 comments

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)));
        }
    }

OTO8056 avatar Dec 20 '22 19:12 OTO8056

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).

Drjacky avatar Dec 21 '22 11:12 Drjacky