为什么我的裁剪图像很小?
Why is my cropped image little?
我正在尝试编写一个代码,允许用户 select 画廊中的图片并使用 Intent 裁剪它,以便最终在 ImageView 中显示它(用于个人资料图片目的)。
一切正常,图像可以 select 编辑,可以裁剪并显示在自定义对话框中的 ImageView 中。问题是我的自定义对话框中的 ImageView 非常小,即使我将宽度和高度设置为 wrap_content,所以用户几乎看不到裁剪后的图像。
截图如下:Little image
代码:
我的自定义对话框 (custom_dialog_pp.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/style_layout_rounded_white">
<TextView
android:id="@+id/useit_customdialog_pp_TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:text="@string/usethispic"
android:textSize="25sp"
android:textColor="@color/white"
android:background="@drawable/style_tv_toproundcorner"/>
<ImageView
android:contentDescription="@string/profilpic"
android:id="@+id/pp_customdialog_pp_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/cancel_customdialog_pp_BTN"
android:background="@drawable/style_button_primary"
android:text="@string/cancel"
/>
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/done_customdialog_pp_BTN"
android:background="@drawable/style_button_primary"
android:text="@string/done"
/>
</LinearLayout>
在我的 MainActivity 中:
////////////////////
ChangeProfilPic.onClickListener {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 2);
}
////////////////////
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 4 && resultCode == RESULT_OK && data != null) {
Bundle extras = data.getExtras();
Bitmap selectedImage = extras.getParcelable("data");
android.support.v7.app.AlertDialog.Builder mBuilderLoading = new android.support.v7.app.AlertDialog.Builder(MainActivity.this);
View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null);
final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV);
pp_customdialog_pp_IV.setImageBitmap(selectedImage);
mBuilderLoading.setView(mViewLoading);
final android.support.v7.app.AlertDialog dialogLoading = mBuilderLoading.create();
dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogLoading.show();
} else if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
ImageCropFunction(data.getData());
}
}
public void ImageCropFunction(Uri imguri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(imguri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, 4);
} catch (ActivityNotFoundException ignored) {
}
Ps:我想保留裁剪选项
我相信默认行为是 return return 结果中的缩略图。因此您可能还需要使用 URI 在 return 上获取全尺寸图像。
像这样改变你的onActivityResult()
。
您返回的是缩略图 (128x128)。您必须使用 Uri
才能获得完整图像。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
if (selectedImage == null) {
return;
}
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null) {
return;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
if (picturePath != null) {
Log.d("TAG", "picturePath " + picturePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
if(bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// you can change the quality here. for dialog, you might only want 50 instead of 100..
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
AlertDialog.Builder mBuilderLoading = new AlertDialog.Builder(MainActivity.this);
View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null);
final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV);
pp_customdialog_pp_IV.setImageBitmap(bitmap);
mBuilderLoading.setView(mViewLoading);
final AlertDialog dialogLoading = mBuilderLoading.create();
dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogLoading.show();
} else {
Log.e("TAG", "bitmap is null?!");
}
}
}
}
我正在尝试编写一个代码,允许用户 select 画廊中的图片并使用 Intent 裁剪它,以便最终在 ImageView 中显示它(用于个人资料图片目的)。
一切正常,图像可以 select 编辑,可以裁剪并显示在自定义对话框中的 ImageView 中。问题是我的自定义对话框中的 ImageView 非常小,即使我将宽度和高度设置为 wrap_content,所以用户几乎看不到裁剪后的图像。
截图如下:Little image
代码:
我的自定义对话框 (custom_dialog_pp.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/style_layout_rounded_white">
<TextView
android:id="@+id/useit_customdialog_pp_TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:text="@string/usethispic"
android:textSize="25sp"
android:textColor="@color/white"
android:background="@drawable/style_tv_toproundcorner"/>
<ImageView
android:contentDescription="@string/profilpic"
android:id="@+id/pp_customdialog_pp_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/cancel_customdialog_pp_BTN"
android:background="@drawable/style_button_primary"
android:text="@string/cancel"
/>
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/done_customdialog_pp_BTN"
android:background="@drawable/style_button_primary"
android:text="@string/done"
/>
</LinearLayout>
在我的 MainActivity 中:
////////////////////
ChangeProfilPic.onClickListener {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 2);
}
////////////////////
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 4 && resultCode == RESULT_OK && data != null) {
Bundle extras = data.getExtras();
Bitmap selectedImage = extras.getParcelable("data");
android.support.v7.app.AlertDialog.Builder mBuilderLoading = new android.support.v7.app.AlertDialog.Builder(MainActivity.this);
View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null);
final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV);
pp_customdialog_pp_IV.setImageBitmap(selectedImage);
mBuilderLoading.setView(mViewLoading);
final android.support.v7.app.AlertDialog dialogLoading = mBuilderLoading.create();
dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogLoading.show();
} else if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
ImageCropFunction(data.getData());
}
}
public void ImageCropFunction(Uri imguri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(imguri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, 4);
} catch (ActivityNotFoundException ignored) {
}
Ps:我想保留裁剪选项
我相信默认行为是 return return 结果中的缩略图。因此您可能还需要使用 URI 在 return 上获取全尺寸图像。
像这样改变你的onActivityResult()
。
您返回的是缩略图 (128x128)。您必须使用 Uri
才能获得完整图像。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
if (selectedImage == null) {
return;
}
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null) {
return;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
if (picturePath != null) {
Log.d("TAG", "picturePath " + picturePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
if(bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// you can change the quality here. for dialog, you might only want 50 instead of 100..
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
AlertDialog.Builder mBuilderLoading = new AlertDialog.Builder(MainActivity.this);
View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null);
final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV);
pp_customdialog_pp_IV.setImageBitmap(bitmap);
mBuilderLoading.setView(mViewLoading);
final AlertDialog dialogLoading = mBuilderLoading.create();
dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogLoading.show();
} else {
Log.e("TAG", "bitmap is null?!");
}
}
}
}