[英]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.