2
votes

I am going through http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/ and trying to get the Local Images working with Android however I am experiencing some issues when attempting to do something in normal monodroid just as a further test. I'm using a SharedProject, just for reference.

I have added the test image at Resources/drawable (test1.png), and also setting the Build Action as AndroidResource as it describes, and if I do the following in Xamarin.Forms it works:-

Xamarin.Forms.Image myimage = new Image();
myimage.Source = ImageSource.FromFile("test1.png");

However, if I try and retrieve the same file via the following it comes back as null.

 Android.Graphics.Bitmap objBitmapImage = Android.Graphics.BitmapFactory.DecodeFile("test1.png")

Does anyone know why this is null and how to correct?

3

3 Answers

3
votes

The Xamarin.Forms FileImageSource has a fallback mode as it covers two different scenarios.

First it will check whether the file exists in the file system via the BitmapFactory.DecodeFile.

Should the file not exist as specified in the File property, then it will use BitmapFactory.DecodeResource to try and load the resource content as a secondary alternative.

FileImageSource for Android therefore isn't only just for files that exist in the file system but also for retrieving named resources also.

This link indicates that Android Resources are compiled into the application and therefore wouldn't be part of the file system as physical files.

0
votes

Since test1.png is a Drawable you should use BitmapFactory.DecodeResource ()

0
votes
string scr = "@drawable/test1.png";
ImageView iv = new ImageView(context);
Bitmap bm = BitmapFactory.DecodeFile(src);
if (bm != null)
    iv.SetImageBitmap(bm);