[英]Android Image Gallery Crop Image and set ImageView
i am having some issues with setting an ImageView with the object returned from the gallery intent. 我在用图库意图返回的对象设置ImageView时遇到一些问题。 I must be doing something wrong but when i debug it, everything seems to be in its right place.
我必须做错了什么,但是当我调试它时,一切似乎都在正确的位置。 It doesn't throw any exceptions, It just changes my image to nothing.
它不会引发任何异常,它只会改变我的图像。
I was trying to follow other guides and tutorials on stackoverflow to get my answer but i cant get it working. 我试图遵循有关stackoverflow的其他指南和教程来获取我的答案,但我无法使其正常运行。
this calls the gallery fine, But it doesn't allow croping... 这可以说画廊不错,但是不允许修剪...
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.
}
}
}
I don't know what i am missing, the the ImageView is restricted to 150dp x 150dp 我不知道我在想什么,ImageView被限制为150dp x 150dp
I have tried changing it to a known resource which works fine, but using this method just fails every time, no matter what i attempt. 我尝试将其更改为可以正常工作的已知资源,但是无论我尝试如何,每次使用此方法都会失败。
crop image in android by using com.android.camera.action.CROP
. 使用
com.android.camera.action.CROP
在android中裁剪图像。 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: 当图片选择活动返回时,将选择保存内容。在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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.