I’ve tried answers from other similar stackoverflow questions, but cannot figure out what i am doing wrong. I have an sqlite database which includes several columns, one of which is the filename of a png file stored in res/drawable. I can populate my listview with a list of items from sqlite, and on click i can display all the details stored for that chosen item – EXCEPT that the image never displays. If i simply display the name of the image file, it displays the correct name for that chosen item. But i cannot get the image itself to display.
If I hard-code the image src file into the layout xml, it displays correctly, but, i need to be able to change the image depending upon which item is selected.
Here are three ways I have tried to accomplish this (with a hard-coded name for example simplification using my res/drawable/acorn.png file). None of these works. All the rest of the correct item detail info displays, but no image. Any ideas what i am doing wrong here?
The layout xml includes:
<ImageView
android:id="@+id/wordimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:maxHeight="200dp"
android:scaleType="center"
android:contentDescription="image for this word"
/>
attempt 1:
int imageid = getResources().getIdentifier("com.brohoward.androidapps.itzadatabase:drawable/acorn.png", null, null);
ImageView imagenow = (ImageView) findViewById(R.id.wordimage);
imagenow.setImageDrawable(getResources().getDrawable(imageid));
attempt b:
int imageid = getResources().getIdentifier("com.brohoward.androidapps.itzadatabase:drawable/acorn.png", null, null);
ImageView imagenow = (ImageView) findViewById(R.id.wordimage);
imagenow.setImageResource(imageid);
attempt c:
ImageView imagenow = (ImageView) findViewById(R.id.wordimage);
imagenow.setImageBitmap(BitmapFactory.decodeFile("com.brohoward.androidapps.itzadatabase:drawable/acorn.png"));
thanks in advance!
Don’t include the .png in the call to getIdentifier:
Then, set the ImageView using ImageView#setImageResource: