Android Studio中使用Glide加载字符串预览图至ImageView的问题
解决方案
你之前的Glide代码核心错误是把ImageView对象(previewUrl)传给了load()方法,而load()需要的是图片地址字符串image.previewUrl;另外不要手动实例化Activity(),直接用当前Activity的上下文this即可。
正确的Glide加载代码
替换你注释掉的所有尝试代码,用这一行即可:
Glide.with(this).load(image.previewUrl).into(previewUrl)
额外注意事项
- 确认
ImageModel中的previewUrl是有效的网络图片地址或本地文件路径字符串 - 如果加载的是网络图片,必须在
AndroidManifest.xml中添加网络权限:<uses-permission android:name="android.permission.INTERNET" /> - 确保项目已正确引入Glide依赖(以最新稳定版本为例):
implementation 'com.github.bumptech.glide:glide:4.16.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
修正后的完整代码片段
class ImageInformation : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_image_information) val image = intent.getParcelableExtra<ImageModel>("image") if (image != null){ val downloadUrl: TextView = findViewById(R.id.downloadUrla) val previewUrl : ImageView = findViewById(R.id.previewUrls) val id: TextView = findViewById(R.id.idsa) val filename: TextView = findViewById(R.id.fileNamea) val filesize: TextView = findViewById(R.id.filesizea) val contentType: TextView = findViewById(R.id.contentTypea) val createdBy: TextView = findViewById(R.id.createdBya) val createdTimestamp: TextView = findViewById(R.id.createdTimestampa) val creationSource: TextView = findViewById(R.id.creationSourcea) val domainIdentityType: TextView = findViewById(R.id.domainIdentityTypea) val domainIdentityValue: TextView = findViewById(R.id.domainIdentityValuea) val tags: TextView = findViewById(R.id.tagsa) val description: TextView = findViewById(R.id.descriptiona) downloadUrl.text = image.downloadUrl // 正确加载图片的代码 Glide.with(this).load(image.previewUrl).into(previewUrl) id.text = image.id filename.text = image.fileName filesize.text = image.filesize.toString() contentType.text = image.contentType createdBy.text = image.createdBy createdTimestamp.text = image.createdTimestamp creationSource.text = image.creationSource domainIdentityType.text = image.domainIdentityType domainIdentityValue.text = image.domainIdentityValue tags.text = image.tags.toString() description.text = image.description } } }
内容的提问来源于stack exchange,提问作者En0309O19kl93it
相关产品推荐
相关产品推荐

