Question

I have an Android app which displays a collection of data in an ExpandableListView. The expandable list view has only one child. In that child I am adding several TextView elements, and after the last one an ImageView.

That ImageView is the problem.

If I set the image resource from the drawable folder using

R.drawable.test_image
it shows like this:

Image added from Resource

But if I add the image programmatically, like this:

ImageView drinkImage = (ImageView) inflater.inflate(R.layout.drink_image, null, false);
// FileString is "/storage/sdcard0/.WhatToDrink/drinkImages/1388450237870.jpg" 
Drawable drinkImageDrawable = Drawable.createFromPath(fileString);

drinkImage.setImageDrawable(drinkImageDrawable);

It shows like this:

Added from file

Identical file. I grabbed the file itself from the storage and moved it to my drawable folder and adding it that way works perfectly.

ImageView xml code:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentDescription="Drink Image"
    android:id="@+id/drink_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

ExpandableListView XML Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bluelimecrew.whattodrink.DrinkListActivity.DrinkListFragment">
    <LinearLayout
            android:id="@+id/listContainer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical">
        <ExpandableListView
                android:id="@+id/drinkList"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
        </ExpandableListView>
    </LinearLayout>

    <Button
            android:id="@+id/backToDashboard"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/nextButtons"
            android:text="@string/backToDashboard" />
</LinearLayout>

Any ideas?

I'm running this on a Galaxy Nexus running Android 4.1.1

Was it helpful?

Solution

Drawable.createFromPath will not have the context information thus cannot scale the bitmap properly (because it does not know screen density etc).

As you can see in the source code, it will call drawableFromBitmap(null, bm, null, null, pathName). The first parameter (null) is the resources reference.

Instead, use a constructor that receives the Context or Resources so that it can scale it bitmap properly.

Check new BitmapDrawable(Resources res, String path)

You can see that the BitmapDrawable constructors which do not receive the resources are deprecated.

OTHER TIPS

not sure, because images can be tricky, but try this

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentDescription="Drink Image"
    android:id="@+id/drink_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top