[英]View an image with xamarin forms
I should be viewing an image from internal memory.我应该查看内存中的图像。
Code:代码:
public partial class MainPage : ContentPage
{
static readonly File file = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "tmp.jpg");
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
bool exists = file.Exists(); // exists = true
ImageExample.Source = ImageSource.FromFile(file.Path);
}
}
xaml: xml:
<StackLayout>
<Button HeightRequest="100"
WidthRequest="200"
HorizontalOptions="Center"
VerticalOptions="Center"
Clicked="Button_Clicked"/>
<Image x:Name="ImageExample"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
WidthRequest="200"
HeightRequest="100"/>
</StackLayout>
Why is the image not displayed?为什么图片不显示?
Thanks in advance!提前致谢!
I figured it out.我想到了。 It looks like your app doesn't have permissions to read from file storage.
您的应用似乎无权从文件存储中读取数据。
You should add the line您应该添加该行
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
into the AndroidManifest.xml
file (located in Properties
subfolder of the <your-app-name>.Android
folder).进入
AndroidManifest.xml
文件(位于<your-app-name>.Android
文件夹的Properties
子文件夹中)。
Also, you should add to the MainActivity.cs
file:此外,您应该添加到
MainActivity.cs
文件:
using Android;
using Android.Support.V4.App;
....
const int OurRequest = 2;
protected override void OnCreate(Bundle savedInstanceState)
{
....
var permission = new string[1];
if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != Permission.Granted)
{
permission[0] = Manifest.Permission.ReadExternalStorage;
ActivityCompat.RequestPermissions(this, permission, OurRequest);
}
Now if you run the app, it will ask about permission and (in case of positive answer, obviously) image will be loaded.现在,如果您运行该应用程序,它会询问许可,并且(显然是肯定的回答)图像将被加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.