簡體   English   中英

如何從本地主機服務器加載圖像到 imageview android

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

我在我的 android 應用程序中將圖像從本地主機加載到 imageView 時遇到問題 我嘗試使用 picasso 和 glid 但它們都不起作用。 圖像存儲在我的電腦上的 xampp 中,我將我的物理設備連接到我的電腦並使用 usb 網絡共享來訪問本地主機。 問題不在於手機和設備之間的連接,因為我能夠插入服務器上的數據庫,而且我還可以使用 ipv4 地址訪問我設備上的本地主機。 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)

這也是我嘗試了我在上面的代碼中使用的這個鏈接,它正確地獲取了圖像,如下圖所示在此處輸入圖像描述 所以如果有人知道解決方案請幫忙

使用 Picasso,您可以嘗試此解決方案

我在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))
    }
}

顯然,只要您使用未經認證的本地服務器,它就會被使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM