jrmgx
jrmgx

Reputation: 729

My android imageView is black no matter what

I try to display an ImageView into my app on a well (hopefully) defined view (XML)

<?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" />

<!-- ... -->

and after that in my onCreate method on the activity

    @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();
}

with cream.getMedia() returning a valid byte[] (from database) which I can log giving me:

07-18 17:41:16.812: I/app(9883): byte[] is: [B@414af0b0

But at the end the imageView is black (no image on it) If I don't run the onCreate code it displays the resource set in the XML (on black background as set in XML too)

What is wrong?

Upvotes: 4

Views: 3881

Answers (2)

minhazur
minhazur

Reputation: 4988

It may be because of you are trying to load a larger bitmap into the imageview, please try to scale it down before setting.

Upvotes: 0

Blaaz
Blaaz

Reputation: 91

I run into this problem, in my case the problem came from the config in:

bluid.gradle

shrinkResources true

It deleted my files because I didn't have a static reference to my resources. (images in my case)

so I found this article "Keeping Resources" in this link Resource Shrinking

In resumen it says: you should make a file in this path "/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*"/>

Upvotes: 6

Related Questions