You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio应用中ImageView图片无法显示问题求助

Android ImageView图片无法显示问题排查与解决

问题描述

在Android Studio中创建空Activity,布局文件添加ImageView组件,将图片导入drawable目录后运行应用,图片完全无法显示。相关代码如下:

布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/download" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity文件

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

解决方法

核心问题修正

你的ImageView用了tools:src="@drawable/download",这是问题关键。tools:开头的属性只在Android Studio布局预览里生效,实际运行时系统不会加载这个属性指向的资源。把它换成Android标准命名空间的android:src即可:

修改后的ImageView代码:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:src="@drawable/download" />

若修改后仍不显示,可排查这些点

  • 检查图片命名:文件名只能用小写字母、数字、下划线,不能有大写、空格或特殊字符,否则资源无法被识别。
  • 确认目录位置:图片要放在res/drawable或对应密度的drawable-xxxhdpi等目录,别误放到drawable-v24这类特定版本目录,避免低版本设备无法访问。
  • 清理项目缓存:点击Android Studio顶部菜单栏File > Invalidate Caches... > Invalidate and Restart,清缓存后重新编译运行。
  • 检查图片格式:确保图片是Android支持的格式(PNG、JPG、WebP等),损坏的文件也会导致无法显示。

内容的提问来源于stack exchange,提问作者Lương Hoàng Minh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 15:22:27