3

I am selecting image from a gallery.
This is the code which i am using

            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);
            this.StartActivityForResult(Intent.CreateChooser(intent,
                    "Select Picture"), SelectPicture);

I am getting this string in data.DataString content://media/external/images/media/11 on Activty result..
Which is not a full path to the selected Image. But ultimately i want to convert it to bitmap.

In activity result..

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)

Bitmap bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");

Gives null error.

How ever instead of selecting image from the gallery when i capture image from camera it works fine and i get the bitmap. Code i am using:

 var cameraIntent = new Intent(MediaStore.ActionImageCapture);
 cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
 this.StartActivityForResult(cameraIntent, TakePicture);
Rizwan Mumtaz
  • 3,875
  • 2
  • 30
  • 31

4 Answers4

7

To convert from Uri to Bitmap follow this example: convertUriToBitmap. In monodroid the code is as follows:

private Android.Graphics.Bitmap NGetBitmap(Android.Net.Uri uriImage)
    {
        Android.Graphics.Bitmap mBitmap = null;
         mBitmap = Android.Provider.MediaStore.Images.Media.GetBitmap(this.ContentResolver, uriImage);
         return mBitmap;
    }
Community
  • 1
  • 1
Daniele75
  • 71
  • 5
0
public override void OnActivityResult(int requestCode, int resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    Uri uri = data.Data;
}
glennsl
  • 28,186
  • 12
  • 57
  • 75
nnsr
  • 43
  • 1
  • 8
  • 2
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Baum mit Augen Sep 20 '17 at 16:20
0

Android

val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri))

Xamarin Android

var ins = context.ContentResolver.OpenInputStream(uri);
Bitmap bitamp = BitmapFactory.DecodeStream(ins);
pseudoankit
  • 309
  • 2
  • 6