Im trying to get an image, "stupidshit.png", that's in my drawable folder of my android projekt as byte array or bitmap in the Code behind of a ContenPage. I tried literally everything. Nothing is working for me...
Asked
Active
Viewed 177 times
-1
-
Have you tried using `BitmapFactory.DecodeResource(Resources, Int32)` ? – hardartcore Oct 07 '21 at 08:06
-
@hardartcore Where do I get "Resources" from? – Kajot Oct 07 '21 at 08:10
-
Check this answer: https://stackoverflow.com/questions/41780588/bitmapfactory-decoderesource-does-not-recognise-drawables – hardartcore Oct 07 '21 at 08:15
-
@hardartcore there is no getContext() – Kajot Oct 07 '21 at 08:19
-
Do you know what is `Context`? You need a valid `Context` object in order to get a `Resources` object and pass it to `BitmapFactory`. – hardartcore Oct 07 '21 at 08:27
-
@hardartcore i tried Android.App.Application.Context but that doesnt have getResource... Im completely lost right now tbh... I dont know where to get a valid context object... – Kajot Oct 07 '21 at 08:32
-
You need a `Context` object from an `Activity` or `Application` class if you have one declared. Where are you trying to call that? In what class? – hardartcore Oct 07 '21 at 08:36
-
@hardartcore I have a ContentPage and that ContentPage has a Button. When I click the Button I want to get the picture for further usage. – Kajot Oct 07 '21 at 08:38
-
Here is how to get Context in Xamarin Forms: https://stackoverflow.com/questions/25613225/get-current-activity-from-application-context-monoandroid/25614348 – hardartcore Oct 07 '21 at 08:47
-
I dont understand that... there is not Forms.Context that i could assign the current activity into... – Kajot Oct 07 '21 at 09:01
1 Answers
0
I have a ContentPage and that ContentPage has a Button. When I click the Button I want to get the picture for further usage.
You can use DependencyService to achieve this function.
please refer to the following code:
1.create an interface in form :
public interface InGetImageService
{
Bitmap GetBitmap();
}
2.implement interface in android:
[assembly: Dependency(typeof(GetImageService))]
namespace DependencyServiceDemos.Droid
{
public class GetImageService : InGetImageService
{
public Bitmap GetBitmap()
{
string imagefileName = "water.png";
// Remove the file extention from the image filename
imagefileName = imagefileName.Replace(".jpg", "").Replace(".png", "");
// Retrieving the local Resource ID from the name
int id = (int)typeof(Resource.Drawable).GetField(imagefileName).GetValue(null);
// Converting Drawable Resource to Bitmap
var originalImage = BitmapFactory.DecodeResource(MainActivity.Instance.Resources, id);
return originalImage;
}
}
}
3.call the following code when clicking button in ContentPage
:
Bitmap bitmap = DependencyService.Get<InGetImageService>().GetBitmap();
Note:
The MainActivity.Instance
is a variable in MainActivity
in android,please refer to the following code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static MainActivity Instance { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Instance = this;// assign a value
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
}

Jessie Zhang -MSFT
- 9,830
- 1
- 7
- 19