选取图像后 CropActivity 不打开
CropActivity doesn't open after picking image
我厌倦了在 https://www.simplifiedcoding.net/crop-image-android-tutorial/ 中做同样的事情。从图库中选择图像并裁剪后。但是当我将它包含在我的片段中时,它是开放的画廊,但之后不显示作物 activity。但是当我直接在 activity 中做同样的事情时,它的工作!
我的片段 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);
}
所以我解决了这个问题做下一步。
我刚刚创建了辅助 Activity (空)并在 activity 工作后调用裁剪函数。问题出在上下文和活动中。
我厌倦了在 https://www.simplifiedcoding.net/crop-image-android-tutorial/ 中做同样的事情。从图库中选择图像并裁剪后。但是当我将它包含在我的片段中时,它是开放的画廊,但之后不显示作物 activity。但是当我直接在 activity 中做同样的事情时,它的工作!
我的片段 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);
}
所以我解决了这个问题做下一步。 我刚刚创建了辅助 Activity (空)并在 activity 工作后调用裁剪函数。问题出在上下文和活动中。