The activity gets the string (R.drawable.che3) by the method final String get_image = intent.getStringExtra(EXTRA_IMAGE); The question is how do I turn this line into a resource (image or link to a resource) here is an example for this method

 ImageView imageView = (ImageView) findViewById(R.id.backdrop); imageView.setImageResource(R.drawable.cheese_1); 

->

  ImageView imageView = (ImageView) findViewById(R.id.backdrop); imageView.setImageResource(EXTRA_IMAGE); 

or so more understandably what I am about is imageView.setImageResource("R.drawable." + EXTRA_IMAGE )

    1 answer 1

    If you know the ID of the resource, then it is better to transfer it to the int:

     intent.putExtra("EXTRA_IMAGE", R.id.someres); getIntent().getIntExtra("EXTRA_IMAGE", 0); 

    And if you only know the string "someres", then you can convert the string to a resource like this:

     private int getResourceByName(String res) { String packageName = getPackageName(); return getResources().getIdentifier(res, "drawable", packageName); } 

     imageView.setImageResource(getResourceByName(EXTRA_IMAGE)); 
    • , resources parse through json in string. I can write any string name, can you tell me how to do better? This line needs to be parsed in the int or you can specify when sending - djo
    • @jon, then only the second option - katso
    • , and this method is suitable? String mString = "R.id.someres"; // string int mInt = Integer.parseInt (mString); , but for some reason I crash - djo
    • @jon, so will not work - katso