Java Android ImageView 不显示图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9798367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:44:58  来源:igfitidea点击:

Android ImageView Is Not Displaying Image?

javaandroidimageview

提问by comead

Can anyone tell me why the following code is not working? The Image does not appear at all.

谁能告诉我为什么下面的代码不起作用?图像根本不出现。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"

   <ImageView
          android:src="@drawable/splashscreen"
          android:scaleType="center"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

</RelativeLayout>

回答by Kurty

What format is your drawable file in? Android prefers using .PNGmostly.

你的drawable文件是什么格式的?Android 更喜欢使用.PNG

回答by JoxTraex

A null bitmap = unusable image.

空位图 = 无法使用的图像。

This is the basis for how an image is actually used. When an image is used in android and it is assigned it actually converts the image into a bitmap, and depending on certain parameters being size and various other factors, it can fail, which would result in your image being converted and being returned as null due to some failure that occurred.

这是如何实际使用图像的基础。当在 android 中使用图像并分配它时,它实际上将图像转换为位图,并且根据某些参数大小和其他各种因素,它可能会失败,这将导致您的图像被转换并返回为空发生的一些故障。

回答by Android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

回答by kenj

Check for the following:

检查以下各项:

  1. Are you invoking setContentView(for your splash screen) on the UI thread
  2. Are there other activities (or views) that you're displaying too quickly after your splash screen (i.e. nullifying the splash screen setContentView)?
  3. Trying using match_parent instead of wrap_content on the ImageViewlayout_heightand layout_width.
  1. 您是否setContentView在 UI 线程上调用(为您的启动画面)
  2. 是否有其他活动(或视图)在启动画面(即使启动画面无效setContentView)之后显示得太快?
  3. 尝试在ImageViewlayout_height和上使用 match_parent 而不是 wrap_content layout_width

回答by sivag1

Many of the image related issues are solved by using the third party libraries like Picasso by Jake Wharton. I had unexplained issues with image loading and some times even getting out of memory error. That was especially cased by imaged edited (using Gimp for Mac). Picasso does the hard work of handling the memory and necessary caching etc.

许多与图像相关的问题是通过使用第三方库解决的,例如 Jake Wharton 的 Picasso。我遇到了无法解释的图像加载问题,有时甚至出现内存不足错误。图像编辑(使用 Gimp for Mac)尤其如此。毕加索在处理内存和必要的缓存等方面做了艰苦的工作。

http://square.github.io/picasso/

http://square.github.io/picasso/

回答by Kai Wang

Where did you put the source image file? Is that under the Drawable folder? If so, then create mdpi, hdpi, xhdpi, xxhdpi image source for the image, and put them in mdpi, hdpi, xhdpi, xxhdpi folders.

你把源图像文件放在哪里?是在 Drawable 文件夹下吗?如果是,则为该图像创建mdpi、hdpi、xhdpi、xxhdpi图像源,并将它们放在mdpi、hdpi、xhdpi、xxhdpi文件夹中。

回答by Clark Kent

This is the solution that worked for me after checking the layout xml, theme etc. Just changing the "extends Activity" from your java class to "extends AppCompatActivity".

这是在检查布局 xml、主题等之后对我有用的解决方案。只需将“扩展活动”从您的 Java 类更改为“扩展 AppCompatActivity”。

Should some of the previous suggested solutions worked for you, others having the same problem with ImageView image not showing might try this as well.

如果之前建议的一些解决方案对您有用,其他人也可以尝试使用 ImageView 图像未显示的相同问题。

回答by bst91

In my case, all my fragment's imageViews are not showing when I run my application. The problem is, I extend my "Activity" class from "FragmentActivity". so I just change it "FragmentActivity" to "AppCompatActivity"

就我而言,当我运行我的应用程序时,我的所有片段的 imageViews 都没有显示。问题是,我从“ FragmentActivity”扩展了我的“ Activity”类。所以我只是将“ FragmentActivity”更改为“ AppCompatActivity

maybe this will help someone.

也许这会帮助某人。

回答by kokabi

Don'tcreate drawablefolder yourself. Recreate it with Image Assetoption in android studio.

不要自己创建可绘制文件夹。在 android studio 中使用Image Asset选项重新创建它。

In my case, the problem was drawable folder itself. i had created drawable folder myself. it's not correct. in the android studio we should create drawable folder with Image Asset option. i didn't get the reason but now the problem is sloved.

就我而言,问题是可绘制文件夹本身。我自己创建了 drawable 文件夹。这是不正确的。在 android studio 中,我们应该创建带有 Image Asset 选项的 drawable 文件夹。我没有得到原因,但现在问题解决了。

回答by Rahul Makhija

In my case, the image wasn't visible while choosing the image from the list of images.

就我而言,从图像列表中选择图像时,图像不可见。

Rebuilding the project fixed it for me.

重建项目为我修复了它。

Build -> Rebuild Project

构建 -> 重建项目