AssetManager and InputStream for ImageView

This is the part of my function that assigns an image to my ImageView. I can not get it to show an image. I have my files listed under assets/signs/"image names." This code came straight from my textbook with some minor tweaks to fit my code. I appreciate the help.

String nextImage = signNames.remove(0);
AssetManager assets = getAssets();
InputStream stream;
    try{
         stream = assets.open("signs/"+nextImage + ".gif");
         Drawable sign = Drawable.createFromStream(stream,nextImage +".gif" );
         signImageView.setImageDrawable(sign);
    }
    catch(IOException e){
         Log.e(TAG, "Error loading " +nextImage, e);
    }

Answers


I think there is a much simpler idiom for what you are trying to do. No need to stream the image the want to display. Simply put the image in res/drawable (or res/drawable-hdpi, etc). Then simply:

 signImageView.setImageResource(R.drawable.myImage)

Of if you need to get the resource from a dynamic string:

int id = getResources().getIdentifier("yourpackagename:drawable/" + nextImage, null, null);
signImageView.setImageResource(R.drawable.myImage)

see here

This assumes your signImageView is indeed an ImageView.


Need Your Help

Free geo data class library in C++ (Lat / Lng / distance / conversions)

c++ geolocation geocoding geospatial class-library

I am looking for a free geo data class library. I need to convert different formats of Latitude / Longitude values (e.g as here), get distances between lat/lng values, coordinates in range, etc.

aspnet5 rc1 access session outside controller

session controller asp.net-core httpcontext

In .Net Framework 4.5 code, System.Web.HttpContext.Current is used,