我尝试按方面1:4添加具有裁剪功能的Pick图像,它的宽度为2000,高度为500> 1:4,我希望用户选择的图像替换位于drawable / custom_image.jpg中的图像。 注意:图像" custom_image"并未在应用中显示为imageview。 我已经搜索了Stackoverflow,并找到了方法,但我有一个问题,裁剪它&用" R.drawable.custom_image"。
替换所选图像 编辑:忘记裁剪,遇到问题,我只需要在OnActivityResult中替换可绘制代码。我已经创建了一个按钮,当按下它时会调用一个方法,这里是代码:
Button mSelectbutton = (Button) view.findViewById(R.id.selectimage);
mSelectbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SelectImageActivity(v);
}
});
SelectImageActivity:
public void SelectImageActivity(View v){
Intent selectintent = new Intent();
// Show only images, no videos or anything else
selectintent.setType("image/*");
selectintent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(selectintent, "Select Picture"), PICK_IMAGE_REQUEST);
}
以下是方法:
OnActivityResult Code :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Drawable imageView = (Drawable)findViewById(R.drawable.custom_header);
imageView.setImageDrawable(getDrawable(R.drawable.custom_header);
}
}
答案 0 :(得分:1)
我已经使用这个库来解决这个问题,因为保留outputx等..对某些版本的android不起作用:
将此库添加到gradle:
//image cropping library to show as square for android older versions to work
compile 'com.soundcloud.android:android-crop:1.0.1@aar'
此方法执行裁剪:
private void performCrop(Uri mImageCaptureUri) {
//this creates a new image file comment below if you want file creation code
Uri destinationUri = MediaUtils.getOutputMediaFileUri(GlobalConstants.CREATE_IMAGE);
Crop.of(mImageCaptureUri,destinationUri).asSquare().start(getActivity(),this);
}
在您的活动结果中,您可以获得裁剪图像:
if(requestCode == Crop.REQUEST_CROP) {
Uri outPutImageUri = Crop.getOutput(data);
如果您想要任何进一步的信息,请在下方评论