ImageCell仅在Android上不显示图像 [英] ImageCell not displaying image only on Android

查看:101
本文介绍了ImageCell仅在Android上不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试使用共享库在一个非常基本的ListView中显示一个ImageCell来显示图像.该问题仅出现在Android上,iOS版本正在按预期运行.

I've been trying to get an ImageCell to show an image in a very basic ListView for some time now using the Shared library. The problem ONLY appears on Android, the iOS version is working as intended.

PS:我也无法从Internet加载图像,同样仅适用于Android.我敢肯定问题是相同的.我已经在清单中添加了Internet权限,并尝试了所有不同的HttpClientImplementation和SSL/TLS实现选项.

PS: I am having issues loading images from the internet as well, again only for Android. I'm certain the problem is the same. I have already added the Internet permission in the manifest, and have tried all of the different HttpClientImplementation and SSL/TLS implementation options.

我的XAML如下:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TestXamarin.GreetPage">
    <ListView x:Name="listView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ImageCell Text="{Binding Name}" Detail="{Binding Status}" ImageSource="{Binding ImageUrl}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
        
</ContentPage>

类背后的代码是这样的:

The code behind class is like this :

public partial class GreetPage : ContentPage
{
    public GreetPage()
    {
        On<iOS>().SetUseSafeArea(true);

        InitializeComponent();

        listView.ItemsSource = new List<Contact>
        {
            new Contact { Name="John", ImageUrl = "http://lorempixel.com/100/100/people/1" },
            new Contact { Name="Jack", ImageUrl = "http://lorempixel.com/100/100/people/2", Status="Hello guys" },
            new Contact { Name="Jill", ImageUrl = "http://lorempixel.com/100/100/people/3" }
        };
    }       
}

Contact.cs类是这样:

The Contact.cs class is this:

public class Contact
{
    public string Name { get; set; }
    public string Status { get; set; }
    public string ImageUrl { get; set; }
}

推荐答案

您的问题与具有非安全(http)连接的更高版本的Android的限制有关.

Your issue is related with the restrictions on later Android versions with non secure (http) connections.

如果您调试应用程序并查看输出",您将看到如下消息:

If you debug the app and look at the Output you will see a message like this:

图像加载:获取流的错误 http://lorempixel.com/100/100/people/1 :Java. IO.IOException:明文 不允许通过lorempixel.com进行HTTP流量 Java.Interop.JniEnvironment + InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference实例,Java.Interop.JniMethodInfo 方法,Java.Interop.JniArgumentValue * args)[0x00069]在 :0处 Java.Interop.JniPeerMembers + JniInstanceMethods.InvokeAbstractVoidMethod (System.String编码的成员,Java.Interop.IJavaPeerable自我, Java.Interop.JniArgumentValue *参数)[0x00014]在 :0处 Java.Net.HttpURLConnectionInvoker.Connect()[0x0000a]在 :0处 Xamarin.Android.Net.AndroidClientHandler +<> c__DisplayClass44_0.b__0 ()[0x0005a] in:0 at System.Threading.Tasks.Task.InnerInvoke()[0x0000f]在 :0处 System.Threading.Tasks.Task.Execute()[0x00000]在 :0 ---从之前引发异常的位置开始的堆栈跟踪---

Image Loading: Error getting stream for http://lorempixel.com/100/100/people/1: Java.IO.IOException: Cleartext HTTP traffic to lorempixel.com not permitted at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in :0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in :0 at Java.Net.HttpURLConnectionInvoker.Connect () [0x0000a] in :0 at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.b__0 () [0x0005a] in :0 at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in :0 at System.Threading.Tasks.Task.Execute () [0x00000] in :0 --- End of stack trace from previous location where exception was thrown ---

如您所见,您会看到一条消息,提示Cleartext HTTP traffic to lorempixel.com not permitted at

As you can read there a message saying Cleartext HTTP traffic to lorempixel.com not permitted at

最快的解决方案是在应用程序级别的Manifest.xml文件中添加android:usesCleartextTraffic="true":

The quickest solution is to add the android:usesCleartextTraffic="true" at the application level in the Manifest.xml file:

<application android:label="MyApp.Android" android:usesCleartextTraffic="true">
    ...
</application>

但是,上述内容不建议这样做,因为它在安全性方面存在漏洞.最好的解决方案是在Urls中使用https.

But the above is not the recomended as it open a breach in terms of security. The best solution would be to use https in your Urls.

希望这会有所帮助

这篇关于ImageCell仅在Android上不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆