how to get the source of ImageView in order to change it?

后端 未结 7 1839
醉话见心
醉话见心 2020-12-15 06:23

I know that changing the ImageView resource is not big deal just using myImageView.setImageResource(mynewImageDrawable)

but what I want to do is to chec

7条回答
  •  囚心锁ツ
    2020-12-15 06:49

    You can use this.

     private Drawable colocaImgen(String nombreFile) {
        Drawable res1 = null;
        String uri1 = null;
        try {
            //First image
            uri1 = "@drawable/" + nombreFile;
            int imageResource1 = getResources().getIdentifier(uri1, null,getApplicationContext().getPackageName());
            res1 = getResources().getDrawable(imageResource1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
            res1 = getResources().getDrawable(R.drawable.globo2);// Default image
        }
        return res1;
    }
    

    Then

    ((ImageView) findViewById(R.id.youNameFile)).setImageDrawable(colocaImgen("NameFile"));
    

提交回复
热议问题