我的 android imageView 无论如何都是黑色的

jrm*_*mgx 4 android android-imageview

我尝试在我的应用程序中(希望)定义良好的视图(XML)上显示 ImageView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activityShowLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#73C310"
android:orientation="vertical"
tools:context=".ShowActivity" >

<ImageView
    android:id="@+id/mainImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/moodButton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_margin="12dp"
    android:background="#000"
    android:src="@drawable/gs_04_pic" />

<!-- ... -->
Run Code Online (Sandbox Code Playgroud)

然后在我的活动的 onCreate 方法中

    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_show);

    /* ... blabla bla ... */

    ImageView imageView = (ImageView) findViewById(R.id.mainImage);

    Bitmap bitmap = BitmapFactory.decodeByteArray(cream.getMedia(), 0, cream.getMedia().length);
    Drawable draw = new BitmapDrawable(getResources(), bitmap);
    imageView.setImageDrawable(draw);
    imageView.invalidate();
}
Run Code Online (Sandbox Code Playgroud)

Cream.getMedia() 返回一个有效的 byte[] (来自数据库),我可以记录给我:

07-18 17:41:16.812: I/app(9883): 字节[] 是: [B@414af0b0

但最后 imageView 是黑色的(上面没有图像)如果我不运行 onCreate 代码,它会显示 XML 中设置的资源(也在 XML 中设置的黑色背景上)

怎么了?

小智 6

我遇到了这个问题,就我而言,问题来自以下配置:

蓝色.gradle

shrinkResources true
Run Code Online (Sandbox Code Playgroud)

它删除了我的文件,因为我没有对我的资源的静态引用。(我的例子中的图像)

所以我在这个链接资源收缩中找到了这篇文章“保留资源”

在简历中它说:你应该在这个路径“/res/raw/keep.xml”中创建一个文件

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
  tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"/>
Run Code Online (Sandbox Code Playgroud)