简体   繁体   English

使用glide显示多张图片

[英]Using glide to display multiple images

For a scanning ticket app, I want to display full screen image.对于扫描票应用程序,我想显示全屏图像。 The app does sequential operation.该应用程序执行顺序操作。 You scan your QrCode (first image), the device reach server (second image indicating to wait), then I display a third image if the ticket is validate, else I display the last one.你扫描你的二维码(第一张图片),设备到达服务器(第二张图片表示等待),然后如果票有效我显示第三张图片,否则我显示最后一张。 I'm using glide and the image path is indicated in a second activity, I transmit URI to main activity with a sharedPreferences file.我正在使用 glide 并且图像路径在第二个活动中指示,我使用 sharedPreferences 文件将 URI 传输到主要活动。

Here is the issue:这是问题所在:

val settings = getSharedPreferences("PrefFile", 0)
    if (S_mod){//priority on S_mod
        uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
        uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
        uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
        uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
        displayUri( uri_State_init, background)
        image.setImageResource(R.drawable.empty)
    }

where displayUri is displayUri 在哪里

fun displayUri( uri: Uri?, dir:ImageView){//use to display image known by uri, only for full screen
    Glide.with(this)
        .load(uri)
        .error(R.drawable.imagenotfound)  // image in case of error
        .override(1280, 800) // resizing if user doesn't respect the indicated dim
        .centerCrop()//type of resizing
        .into(dir)
}

The issue is that only the first image called by displayUri is displayed, the other call show the error image (imagenotfound)问题是只显示 displayUri 调用的第一个图像,另一个调用显示错误图像 (imagenotfound)

It seem that I've not totally understood the glide extension.看来我还没有完全理解滑行扩展。

If someone know about this particular issue thanks a lot!!如果有人知道这个特殊问题,非常感谢!

Here这里

val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
    uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
    uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
    uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
    uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
    displayUri( uri_State_init, background)
    image.setImageResource(R.drawable.empty)
}

displayUri( uri_State_init, background) displayUri( uri_State_init, 背景)

You are just calling the display Image function with the same uri which means if you didn't passed the uri, then uri will be null and you will definitely get errors.您只是使用相同的 uri 调用显示图像 function,这意味着如果您没有传递 uri,则 uri 将为 null,您肯定会出错。 What you can do is to send uri like this,你可以做的是像这样发送uri,

val settings = getSharedPreferences("PrefFile", 0)

if (S_mod){//priority on S_mod

    uri = Uri.parse(settings.getString("main_uri", "none"))

    displayUri( uri_State_init, background)

    image.setImageResource(R.drawable.empty)

}

Just change the value of main_uri in Shared Preferences whenever you want to change the state. You can also use onSharedPreferencesChanged Listener.每当您想更改 state 时,只需在共享首选项中更改main_uri的值即可。您也可以使用onSharedPreferencesChanged监听器。

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

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