CropActivity在选择图像后无法打开

问题描述 投票:0回答:1

我厌倦了和https://www.simplifiedcoding.net/crop-image-android-tutorial/一样的事情。从库中选择图像,然后裁剪。但是,当我将其包含到我的片段中时,它是开放库,但是之后没有显示作物活动。但是当我直接在活动中做同样的事情时,它的工作就可以了!

我的片段onViewCreated

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SetupUI(view);
        SetupProfile();
        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();
        FragmentAdapterProfile adapter = new FragmentAdapterProfile(getActivity(), getActivity().getSupportFragmentManager());
        // Set the adapter onto the view pager
        pager.setAdapter(adapter);
        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.profileTabs);
        tabLayout.setupWithViewPager(pager);
        tabLayout.getTabAt(0).setText("About");
        tabLayout.getTabAt(1).setText("Setting");
        tabLayout.getTabAt(2).setText("Post");
        changeavatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    onSelectImageClick(v);
            }
        });
    }

采摘

public void onSelectImageClick(View view) {
        CropImage.startPickImageActivity((Activity) context);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        // handle result of pick image chooser
        if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            Uri imageUri = CropImage.getPickImageResultUri(context, data);

            // For API >= 23 we need to check specifically that we have permissions to read external storage.
            if (CropImage.isReadExternalStoragePermissionsRequired(context, imageUri)) {
                // request permissions and handle the result in onRequestPermissionsResult()
                mCropImageUri = imageUri;
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            } else {
                // no permissions required or already grunted, can start crop image activity
                startCropImageActivity(imageUri);
            }
        }

        // handle result of CropImageActivity
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                avatar.setImageURI(result.getUri());
                Toast.makeText(context, "Cropping successful, Sample: " + result.getSampleSize(), Toast.LENGTH_LONG).show();
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Toast.makeText(context, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // required permissions granted, start crop image activity
            startCropImageActivity(mCropImageUri);
        } else {
            Toast.makeText(context, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * Start crop image activity for the given image.
     */
    private void startCropImageActivity(Uri imageUri) {
        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMultiTouchEnabled(true)
                .setAspectRatio(1,1)
                .start((Activity) context);
    }
java android image crop
1个回答
0
投票

所以我建议将此问题放在下一个地方。我刚刚创建了辅助Activity(空),并在其工作后在Activity中调用了裁剪功能。问题出在上下文和活动中。

© www.soinside.com 2019 - 2024. All rights reserved.