添加 Pickimage,用 1:4 裁剪并替换 drawab.image - 如何?
Add Pickimage, Crop with 1:4 & Replace drawab.image - Howto?
我正在尝试添加 Pick image with Crop feature by aspect 1:4,宽度为 2000,高度为 500 => 1:4,我希望用户选择的图像替换图像位于 drawable/custom_image.jpg。
注意:图像 "custom_image" 未在应用程序中显示为 imageview。
我搜索了 Whosebug,找到了方法,但我在裁剪它并将所选图像替换为 "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);
}
});
选择图像活动:
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);
}
}
我已经使用这个库来解决这个问题,因为保留 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);
}
在您的 activity 结果中,您可以捕捉到裁剪图像:
if(requestCode == Crop.REQUEST_CROP) {
Uri outPutImageUri = Crop.getOutput(data);
如果您想了解更多信息,请在下方评论
我正在尝试添加 Pick image with Crop feature by aspect 1:4,宽度为 2000,高度为 500 => 1:4,我希望用户选择的图像替换图像位于 drawable/custom_image.jpg。 注意:图像 "custom_image" 未在应用程序中显示为 imageview。 我搜索了 Whosebug,找到了方法,但我在裁剪它并将所选图像替换为 "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);
}
});
选择图像活动:
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);
}
}
我已经使用这个库来解决这个问题,因为保留 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);
}
在您的 activity 结果中,您可以捕捉到裁剪图像:
if(requestCode == Crop.REQUEST_CROP) {
Uri outPutImageUri = Crop.getOutput(data);
如果您想了解更多信息,请在下方评论