Hello,
Welcome to our Microsoft Q&A platform!
I used your code, I got the same exception as yours.
I read the Image from URL, use Async/await
method to read it, if you get exception, because you do not get all the byte[]
completely, then you execute Bitmap bitmap1= BitmapFactory.DecodeByteArray(imageBytes1, 0, imageBytes1.Length);
bitmap1 is empty, List<byte[]> images
is empty as well, so you need to wait the image read completely, then this exception will disappear.
I use var result = Task.Run(async () => await Download("https://news.microsoft.com/wp-content/uploads/2014/12/452292672.jpg")).Result;
to wait the Async/await
method execute completely.
List<byte[]> images;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
images = new List<byte[]>();
AddData();
MotionDetected(images);
}
public async void AddData()
{
var result = Task.Run(async () => await Download("https://news.microsoft.com/wp-content/uploads/2014/12/452292672.jpg")).Result;
var result2 = Task.Run(async () => await Download("https://news.microsoft.com/wp-content/uploads/2014/12/452292672.jpg")).Result;
images.Add(result);
images.Add(result2);
}
public async Task<byte[]> Download(string url)
{
using (HttpClient client = new HttpClient())
{
byte[] fileArray = await client.GetByteArrayAsync(url);
return fileArray;
}
}
public bool MotionDetected(List<byte[]> images)
{
byte[] imageBytes1 = images[0];
byte[] imageBytes2 = images[0];
Bitmap bitmap1= BitmapFactory.DecodeByteArray(imageBytes1, 0, imageBytes1.Length);
Bitmap bitmap2 = BitmapFactory.DecodeByteArray(imageBytes2, 0, imageBytes2.Length);
int heightFirstImage=bitmap1.Height;
int widthFirstImage = bitmap1.Width;
int heightSecondImage = bitmap2.Height;
int widthSecondImage = bitmap2.Width;
//ImageProcessing
AssetManager assets = this.Assets;
return true;
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.