简体   繁体   English

Android Image Gallery裁剪图像并设置ImageView

[英]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.

相关问题 从图库中裁剪图像并将其设置为我的imageview背景Android - Crop Image from gallery and set it as my imageview background Android 如何从图库中裁剪图像并在imageview中设置 - how to crop image from gallery and set in imageview android:从图库中选择图片,然后裁剪并显示在图片视图中……但不保存在sharedpreferce中 - android:select image from gallery then crop that and show in an imageview…but not saving in sharedpreferce android:从图库中选择图像然后裁剪并在图像视图中显示 - android:select image from gallery then crop that and show in an imageview Android:裁剪图像并在ImageView上显示 - Android: Crop image and Displaying it on ImageView 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 无法将图像从Android中的图片库设置为imageview - Can't set the image to imageview from image gallery in Android 从图库或相机设置图像以适合Imageview android - set image from Gallery or Camera to fit to Imageview android 单击图像将在ImageView中设置的图像打开到图库中 - Open image set in ImageView into the Gallery on clicking on image 中心裁剪图像以在ImageView上设置适当的尺寸 - center Crop Image In proper size to set on ImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM