Załaduj obraz z folderu zasoby

Próbuję załadować obraz z folderu asset, a następnie ustawić go na ImageView. Wiem, że jest dużo lepiej, jeśli używam R.id.* do tego, ale założenie jest takie, że nie znam id obrazu. Zasadniczo próbuję dynamicznie załadować obraz za pomocą jego nazwy pliku.

Na przykład, losowo pobieram element database reprezentujący powiedzmy 'krowa', Teraz moja aplikacja zrobiłaby wyświetlenie obrazu 'krowa' za pomocą ImageView. Dotyczy to również wszystkich element w database. (Założenie jest takie, że dla każdego elementu istnieje równoważny obraz)

Z góry dzięki.

EDIT

Zapomniałeś o pytaniu, jak załadować obrazek z folderu asset?

Author: biegleux, 2012-07-31

6 answers

Jeśli znasz nazwę pliku w kodzie, wywołanie tego nie będzie problemem:

ImageView iw= (ImageView)findViewById(R.id.imageView1);  
int resID = getResources().getIdentifier(drawableName, "drawable",  getPackageName());
iw.setImageResource(resID);

Nazwa pliku będzie taka sama jak nazwa pliku drawableName, więc nie będziesz musiał zajmować się zasobami.

 29
Author: Erol,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-31 07:04:59

Sprawdź ten Kod . W tym samouczku dowiesz się, jak załadować obraz z folderu zasobu.

/ / Wczytaj obraz

try 
{
    // get input stream
    InputStream ims = getAssets().open("avatar.jpg");
    // load image as Drawable
    Drawable d = Drawable.createFromStream(ims, null);
    // set image to ImageView
    mImage.setImageDrawable(d);
  ims .close();
}
catch(IOException ex) 
{
    return;
}
 101
Author: Chirag,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-29 08:34:19

Tutaj jesteś,

  public Bitmap getBitmapFromAssets(String fileName) {
    AssetManager assetManager = getAssets();

    InputStream istr = assetManager.open(fileName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    return bitmap;
}
 37
Author: osayilgan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-31 07:09:02

Niektóre z tych odpowiedzi mogą odpowiedzieć na pytanie, ale nigdy nie lubiłem żadnej z nich, więc skończyłem pisząc to, to moja pomoc społeczności.

Get Bitmap from assets:

public Bitmap loadBitmapFromAssets(Context context, String path)
{
    InputStream stream = null;
    try
    {
        stream = context.getAssets().open(path);
        return BitmapFactory.decodeStream(stream);
    }
    catch (Exception ignored) {} finally
    {
        try
        {
            if(stream != null)
            {
                stream.close();
            }
        } catch (Exception ignored) {}
    }
    return null;
}

Get Drawable from assets:

public Drawable loadDrawableFromAssets(Context context, String path)
{
    InputStream stream = null;
    try
    {
        stream = context.getAssets().open(path);
        return Drawable.createFromStream(stream, null);
    }
    catch (Exception ignored) {} finally
    {
        try
        {
            if(stream != null)
            {
                stream.close();
            }
        } catch (Exception ignored) {}
    }
    return null;
}
 6
Author: Nicolas Tyler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-12 10:12:17
WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("file:///android_asset/pract_recommend_section1_pic2.png");
web.getSettings().setBuiltInZoomControls(true);
 2
Author: Evgeny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-18 18:38:26
public static Bitmap getImageFromAssetsFile(Context mContext, String fileName) {
        Bitmap image = null;
        AssetManager am = mContext.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
 0
Author: Anil Singhania,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-29 06:24:50