2

I've been searching for the past few days but did not manage to find a solution for my problem. I am currently working on a xamarin Android app. I want to display an image by using the byte array column from by database. I am using another program to find the byte array of a specific photo and after that I insert manually its value in the byte array column from my principal project.

This is my code where I am trying to reproduce the image:

Android.Graphics.Bitmap bitmap=BitmapFactory.DecodeByteArray(currentexercis.image, 0, currentexercis.image.Length);
viewHolder.exercis_photo.SetImageBitmap(bitmap);

Currentexercis.image represents the byte array from my database, and its value seems to be OK, however every time bitmap is null.

This is the code from my other program where I convert the image into bytearray:

Image img = Image.FromFile(opendlg.FileName);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
dbpicEntities1 db = new dbpicEntities1();
db.MyPictures.Add(new MyPicture() { FileName=fileName, Data = ms.ToArray() });
db.SaveChanges();
MessageBox.Show("success");
4
  • In the Java API, decodeByteArray returns null if the image could not be decoded. I assume the same thing happens in the Xamarin library. Commented Apr 8, 2018 at 18:30
  • Thanks, Ted, but since I get the byte array from another program it should be converted back to image with no problem. It`s just the reverse process. Commented Apr 8, 2018 at 18:58
  • 1
    Perhaps you need to use the four-argument version of DecodeByteArray and pass in options that tell the factory how to decode your particular image. Commented Apr 8, 2018 at 19:06
  • I have just added a new button to that secondary program in order to see if the byte array can be used for decode and the image seems to be created , but I am using memory stream in that program and in the android program I cannot use memory stream since my image is of type Android.Widget Image view and the image created with memorystream is of type System Drawing Core Image... Commented Apr 8, 2018 at 19:23

3 Answers 3

2

I think you should use like this.

byte [] imageArray // is your data
MemoryStream mStream = new MemorySteram ();
mStream.write(imageArray,0,imageArray.Length);
Image img = Image.FromStream(mStream);
img.save(filelocation);

Bitmap bitmapimg = BitmapFactory.BitmapFactory.DecodeStream(mStream); 
// if you want to use Bitmap
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your response. However, when I am trying to complete the last line regarding bitmap creating, I need to choose between System Drawing Core and Android Graphics. I have to use Android Graphics, and it seems that it does not have a constructor for Bitmap...
following article link,I think you will find it how to use bitmap.link
0

Byte Array to Image using C# in Xamarin

There are some third-party library which implements this feature quite well like Picasso or Glide.

For Glide, there is official document shows how to use it in Xamarin.Android project: Binding a .JAR. Or you could directly use it from the nuget package:

enter image description here

Then you can code for example like this:

Glide.With(context)
     .Load(imageBytes)
     .Apply(RequestOptions.CircleCropTransform())
     .Into(imageView);

Comments

0

convert to byteArray

 byte[] imgdata = System.IO.File.ReadAllBytes(pathToImage);

convert byte array to bitmap

 private void OnGetMemberAvatarCompleted(byte[] avatarBytes)
{
   var avatarImageView = FindViewById<ImageView>(Resource.Id.memberProfile_avatar);
   if (avatarImageView != null)
   {
      var imageBitmap = BitmapFactory.DecodeByteArray(avatarBytes, 0,avatarBytes.Length);
      RunOnUiThread(() => avatarImageView.SetImageBitmap(imageBitmap));
   }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.