简体   繁体   中英

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. 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. 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. 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)

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 .

I have converted this code in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM