我浏览了很多链接并检查了各种代码,但它还不符合我的要求。
这是我的要求;
问题,在棒棒糖操作系统中,当我从图库中选择图像时它不提供裁剪选项因此getData返回null但是如果我从Photos中选择图像它会裁剪图像并保存它因此getData返回URI但是原始图像被裁剪的那个被覆盖。
以下是代码段..
public void selectImageFromGallery()
{
try
{
Globals g = (Globals) getApplication();
int iWidthDP = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, g.screenWidth, getResources().getDisplayMetrics());
int iHeightDP = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", iWidthDP);
intent.putExtra("outputY", iHeightDP);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_FROM_GALLERY);
}
catch(Exception ex)
{
Toast toast = Toast.makeText(this, "exception occurred in selectImageFromGallery" +
ex.getMessage().toString(), Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try
{
if ((requestCode == PICK_FROM_GALLERY || requestCode == PICK_FROM_CAMERA)
&& resultCode == RESULT_OK && null != data)
{
if (resultCode == RESULT_OK)
{
Bitmap bitmap = null;
Uri selectedImage = null;
if(requestCode == PICK_FROM_GALLERY || requestCode == PICK_FROM_CAMERA) {
selectedImage = data.getData();
if(selectedImage == null)
selectedImage = (Uri)data.getExtras().get("data");
}
if (Build.VERSION.SDK_INT < 19)
{
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();
bitmap = BitmapFactory.decodeFile(picturePath);
}
else
{
ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImage, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
} catch (FileNotFoundException ex) {
Toast toast = Toast.makeText(this, "exception occurred in onActivityResult" + ex.getMessage().toString(), Toast.LENGTH_SHORT);
toast.show();
} catch (Exception ex) {
Toast toast = Toast.makeText(this, "exception occurred in onActivityResult" + ex.getMessage().toString(), Toast.LENGTH_SHORT);
toast.show();
}
}
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageBitmap(bitmap);
}
}
}
catch(Exception ex)
{
Toast toast = Toast.makeText(this, "exception occurred in onActivityResult" + ex.getMessage().toString(), Toast.LENGTH_SHORT);
toast.show();
}
}
}
提前致谢..
答案 0 :(得分:0)
我建议您使用库。这是安装步骤:
首先在build.gradle模块中添加:app
这是在android部分:
repositories {
maven { url "https://jitpack.io" }
}
repositories {
mavenCentral()
maven {
url 'http://lorenzo.villani.me/android-cropimage/'
}
}
这是在依赖项部分:
dependencies {
compile 'com.github.jkwiecien:EasyImage:1.2.1'
compile 'me.villani.lorenzo.android:android-cropimage:1.1.+'
compile 'com.squareup.picasso:picasso:2.5.2'
}
从相机中选择图像
EasyImage.openCamera(Context, 0);
从图库中选择图片
EasyImage.openGallery(Context, 1);
之后。
@Override
protected void onActivityResult( int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() {
@Override
public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) {
//Some error handling
}
@Override
public void onImagePicked(File imageFile, EasyImage.ImageSource source, int type) {
// Picasso.with(MainActivity.this).load(imageFile).into(imageView);
Uri uri = null;
uri = Uri.fromFile(imageFile);
file = imageFile;
CropImageIntentBuilder builder = new CropImageIntentBuilder(200, 200, uri);
builder.setOutlineColor(0xFF03A9F4);
builder.setSourceImage(uri); //data.getData()
startActivityForResult(builder.getIntent(MainActivity.this),100);
}
});
if ((requestCode == 100) && (resultCode == RESULT_OK)) {
// When we are done cropping, display it in the ImageView.
// imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
Picasso.with(MainActivity.this).load(file).into(imageView);
}
}
别忘了在Android Android.manifest中添加它
<activity android:name="com.android.camera.CropImage"/>
这将帮助您通过裁剪功能从图库和相机获取图像,它可以减少您的代码,并且易于维护选择图像。
锄头会帮助你