Я устал делать то же, что и в https://www.simplifiedcoding.net/crop-image-android-tutorial/. Выберите изображение из галереи и после обрезки. Но когда я включил его в свой фрагмент, это открытая галерея, но после не показывает урожайность. Но когда я сделал то же самое непосредственно в деятельности, его работа!
Мой фрагмент на ViewCreated
@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);
}