【问题标题】:How to pick the image from gallery, crop the image and the save image as profile image in xamarin android如何从图库中选择图像,裁剪图像并将图像保存为 xamarin android 中的配置文件图像
【发布时间】:2018-01-17 08:58:38
【问题描述】:

您好,请在这方面帮助我,我想从图库中选择图片并裁剪图片并将图片保存在某个文件夹中

请在这方面帮助我

enter code here

private void ProfilePic_Click(object sender, EventArgs e) { 意图 = 新意图();

        Intent.SetType("image/*");
        Intent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(Intent.CreateChooser(Intent, "EZ-Gift Profile Pic"), PickImageId);
    }


    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
        {
            Android.Net.Uri uri = data.Data;
            //Toast.MakeText(this, path, ToastLength.Long).Show();
            Toast.MakeText(this, uri.ToString(), ToastLength.Long).Show();

            ProfilePic.SetImageURI(uri);

            string path = GetPathToImage(data.Data);

            edit = prefs.Edit();
            edit.PutString("ProfilePicUri", uri.ToString());
            Toast.MakeText(this, uri.ToString(), ToastLength.Long).Show();
            Toast.MakeText(this, path, ToastLength.Long).Show();


        }
    }
    private string GetPathToImage(Android.Net.Uri contentURI)
    {
        ICursor cursor = ContentResolver.Query(contentURI, null, null, null, null);
        cursor.MoveToFirst();
        string documentId = cursor.GetString(0);
        documentId = documentId.Split(':')[1];
        cursor.Close();

        cursor = ContentResolver.Query(
        Android.Provider.MediaStore.Images.Media.ExternalContentUri,
        null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new[] { documentId }, null);
        cursor.MoveToFirst();
        string path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
        cursor.Close();

        return path;
    }

【问题讨论】:

    标签: xamarin.android


    【解决方案1】:

    添加下面的代码来裁剪图像。修改你的 OnActivityResult 代码,试试这样。希望这对您有所帮助。

     protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
            {
                if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
                {
                    Android.Net.Uri uri = data.Data;
                    //Toast.MakeText(this, path, ToastLength.Long).Show();
                    Toast.MakeText(this, uri.ToString(), ToastLength.Long).Show();
    
                    // It will crop the image accordingly.  
                    cropPicture(uri);
    
                    string path = GetPathToImage(data.Data);
    
                    Toast.MakeText(this, uri.ToString(), ToastLength.Long).Show();
                    Toast.MakeText(this, path, ToastLength.Long).Show();
    
    
                }
                else if(requestCode == CROP_PIC)
                {
                    // Get your cropped image here using Bundle and save the bitmap in particular location.
                }
            }
    
     private void cropPicture(Android.Net.Uri picUri)
            {
                try
                {
                    // call the standard crop action intent (the user device may not
                    // support it)
                    Intent cropIntent = new Intent("com.android.camera.action.CROP");
                    // indicate image type and Uri
                    cropIntent.SetDataAndType(picUri, "image/*");
                    // set crop properties
                    cropIntent.PutExtra("crop", "true");
                    // indicate aspect of desired crop
                    cropIntent.PutExtra("aspectX", 2);
                    cropIntent.PutExtra("aspectY", 1);
                    // indicate output X and Y
                    cropIntent.PutExtra("outputX", 256);
                    cropIntent.PutExtra("outputY", 256);
                    // retrieve data on return
                    cropIntent.PutExtra("return-data", true);
                    // start the activity - we handle returning in onActivityResult
                    StartActivityForResult(cropIntent, CROP_PIC);
                }
                // respond to users whose devices do not support the crop action
                catch (ActivityNotFoundException anfe)
                {
                    Toast toast = Toast
                            .MakeText(this, "This device doesn't support the crop action!", ToastLength.Long);
                    toast.Show();
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 1970-01-01
      • 2017-06-25
      • 2018-10-24
      • 2014-10-18
      • 2020-10-08
      • 2012-06-02
      • 1970-01-01
      相关资源
      最近更新 更多