简体   繁体   English

如何从本地主机服务器加载图像到 imageview android

[英]how to load image from localhost server into imageview android

im having probleme loading images from the local host into imageView in my android app i tried using picasso and glid but none of them work.我在我的 android 应用程序中将图像从本地主机加载到 imageView 时遇到问题 我尝试使用 picasso 和 glid 但它们都不起作用。 the images are stored in xampp on my pc, im connecting my phisical device to my pc and using usb tethering to acces the localhost.图像存储在我的电脑上的 xampp 中,我将我的物理设备连接到我的电脑并使用 usb 网络共享来访问本地主机。 the probleme is not in the connection between the phone and the device because iam able to insert into the database on the server and olso i can acces the localhost on my divice using the ipv4 address.问题不在于手机和设备之间的连接,因为我能够插入服务器上的数据库,而且我还可以使用 ipv4 地址访问我设备上的本地主机。 piccasso code val picasso = Picasso.get() picasso.load("http://192.168.107.247/phpFiles/mytaxi/img/mimg.jpg").into(img) glid code Glide.with(this).load("http://192.168.107.247/phpFiles/mytaxi/img/ming.jpg").into(img) piccasso代码val picasso = Picasso.get() picasso.load("http://192.168.107.247/phpFiles/mytaxi/img/mimg.jpg").into(img)滑动代码Glide.with(this).load("http://192.168.107.247/phpFiles/mytaxi/img/ming.jpg").into(img)

also this i tryed this link that im using in the code above and its getting the image correctly as you can see in the image below这也是我尝试了我在上面的代码中使用的这个链接,它正确地获取了图像,如下图所示在此处输入图像描述 so if any one know the solution please help所以如果有人知道解决方案请帮忙

With Picasso you can try this solution .使用 Picasso,您可以尝试此解决方案

I have converted this code in kotlin:我在kotlin中转换了这段代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
 loadLogo("https://192.168.1.112/media/logo.9d457cd5.png")
}

private fun getUnsafeOkHttpClient(): OkHttpClient {
    return try {
        val trustAllCerts: Array<TrustManager> = arrayOf<TrustManager>(
            object : X509TrustManager {
                @Throws(CertificateException::class)
                override fun checkClientTrusted(
                    chain: Array<X509Certificate?>?,
                    authType: String?
                ) {
                }

                @Throws(CertificateException::class)
                override fun checkServerTrusted(
                    chain: Array<X509Certificate?>?,
                    authType: String?
                ) {
                }

                override fun getAcceptedIssuers(): Array<X509Certificate> {
                    return arrayOf()
                }

            }
        )

        val sslContext: SSLContext = SSLContext.getInstance("SSL")
        sslContext.init(null, trustAllCerts, SecureRandom())

        val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory
        val builder = OkHttpClient.Builder()
        builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
        builder.hostnameVerifier { _,_ -> true }
        builder.build()
    } catch (e: Exception) {
        throw RuntimeException(e)
    }
}

fun loadLogo(url: String?) {
    if (url != null && url.isNotEmpty()) {
        val picassoClient = getUnsafeOkHttpClient()
        val picasso = Picasso.Builder(this).downloader(OkHttp3Downloader(picassoClient)).build()
        picasso.isLoggingEnabled = true
        picasso.load(url).into(findViewById<ImageView>(R.id.imageView))
    }
}

Obviously it will be used as long as you use an uncertified local server.显然,只要您使用未经认证的本地服务器,它就会被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从类路径加载Android ImageView的图像? - How to load image for Android ImageView from classpath? 如何从android imageview中的dataurl加载图像? - How to load an image from dataurl in android imageview? 如何在Android中从URL加载没有文件扩展名的图像到ImageView - How to load image without file extension from URL to ImageView in Android 如何从可写文件夹加载图像并分配到Android中的ImageView? - How to load image from writeable folder and assign to ImageView in Android? 如何从Android中的URL将图像加载到动态创建的imageview中? - How to load a image into dynamically created imageview from a URL in android? 如何使用“ setImageURI”功能将图像从数据库加载到android ImageView中? - How to load image from database into android ImageView using “setImageURI” function? Android:将存储中的大图像加载到ImageView中 - Android: Load large image from storage into ImageView 在Android的ExpandableListView中的ImageView处从webUrl加载图像 - Load image from webUrl at ImageView in ExpandableListView in android Android将图像从路径加载到imageView - Android Load image from path to imageView Firebase Android将图像从存储加载到ImageView - Firebase Android Load image to ImageView from storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM