Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 如何从Android应用程序部署的本地文件解码位图_Xamarin_Xamarin.android_Xamarin.forms - Fatal编程技术网

Xamarin 如何从Android应用程序部署的本地文件解码位图

Xamarin 如何从Android应用程序部署的本地文件解码位图,xamarin,xamarin.android,xamarin.forms,Xamarin,Xamarin.android,Xamarin.forms,我正在经历 并且试图让本地图像与Android一起工作,但是作为进一步的测试,我在尝试在普通monodroid中做一些事情时遇到了一些问题。我正在使用SharedProject,仅供参考 我已经在Resources/drawable(test1.png)中添加了测试映像,并按照它的描述将构建操作设置为AndroidResource,如果我在Xamarin.Forms中执行以下操作,它就会工作:- Xamarin.Forms.Image myimage = new Image(); myimage

我正在经历 并且试图让本地图像与Android一起工作,但是作为进一步的测试,我在尝试在普通monodroid中做一些事情时遇到了一些问题。我正在使用SharedProject,仅供参考

我已经在Resources/drawable(test1.png)中添加了测试映像,并按照它的描述将构建操作设置为AndroidResource,如果我在Xamarin.Forms中执行以下操作,它就会工作:-

Xamarin.Forms.Image myimage = new Image();
myimage.Source = ImageSource.FromFile("test1.png");
但是,如果我尝试通过以下命令检索同一个文件,它返回为null

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

有人知道为什么为空以及如何更正吗?

由于test1.png是一个可绘制的文件,您应该使用BitmapFactory.DecodeResource()

Xamarin.Forms FileImageSource具有回退模式,因为它涵盖了两种不同的场景

首先,它将通过
位图工厂.DecodeFile
检查文件系统中是否存在该文件

如果文件不存在属性中指定的文件,则它将使用
BitmapFactory.DecodeResource
尝试加载资源内容作为辅助选择

FileImageSource适用于Android,因此它不仅适用于文件系统中存在的文件,还适用于检索命名资源


这表明Android资源被编译到应用程序中,因此不会作为物理文件作为文件系统的一部分。

当然,这是另一种方式,但从我从文档中看到的,这也是一个物理文件,应该也能被文件位置引用。我如何通过DecodeFile函数来引用它?
string scr = "@drawable/test1.png";
ImageView iv = new ImageView(context);
Bitmap bm = BitmapFactory.DecodeFile(src);
if (bm != null)
    iv.SetImageBitmap(bm);