문제 설명
Android 이미지 갤러리 이미지 자르기 및 ImageView 설정 (Android Image Gallery Crop Image and set ImageView)
갤러리 인텐트에서 반환된 개체로 ImageView를 설정하는 데 몇 가지 문제가 있습니다. 내가 뭔가 잘못하고 있어야 하지만 디버그할 때 모든 것이 올바른 위치에 있는 것 같습니다. 예외를 던지지 않고 내 이미지를 아무 것도 변경하지 않습니다.
제 답변을 얻기 위해 stackoverflow에 대한 다른 가이드 및 자습서를 따르려고 했지만 제대로 작동하지 않습니다.
갤러리를 잘 부르지만 자르기를 허용하지 않습니다...
public void onViewCreated(View view, Bundle savedInstanceState) {
final ImageView profileImage = (ImageView) view.findViewById(R.id.profile_user_image);
profileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
Uri picUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(picUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
// Set The Bitmap Data To ImageView
BitmapFactory.Options o = new BitmapFactory.Options();
o.inPreferredConfig = Bitmap.Config.ARGB_8888;
o.inScaled = true;
ImageView bitmapview = (ImageView) _this.findViewById(R.id.profile_user_image);
bitmapview.setScaleType(ImageView.ScaleType.FIT_XY);
//bitmapview.setImageResource(R.drawable.female_icon); // KNOWN FILE
bitmapview.setImageBitmap(BitmapFactory.decodeFile(filePath,o)); // Gallery File.
}
}
}
내가 무엇을 놓치고 있는지 모르겠습니다. ImageView는 150dp x 150dp로 제한됩니다.
잘 작동하는 알려진 리소스로 변경하려고 시도했지만 이 방법을 사용하는 것은 내가 무엇을 시도하든 매번 실패합니다.
참조 솔루션
방법 1:
crop image in android by using com.android.camera.action.CROP
. after picking image url from gallery.
Intent i = new Intent("com.android.camera.action.CROP");
i.setClassName("com.android.camera", "com.android.camera.CropImage");
File f = new File(filePath);
Uri uri = Uri.fromFile(f);
i.setData(uri);
i.putExtra("crop", "true");
i.putExtra("aspectX", 1);
i.putExtra("aspectY", 1);
i.putExtra("outputX", 96);
i.putExtra("outputY", 96);
i.putExtra("noFaceDetection", true);
intent.putExtra("return‑data", true);
startActivityForResult(intent, REQUEST_CROP_ICON);
When the picture select Activity return will be selected to save the contents.in onActivityResult:
Bundle extras = data.getExtras();
if(extras != null ) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
/ / The stream to write to a file or directly using the
}
(by 173901、vishal jangid)