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