Faced the problem that links in the database to the existing images and audio files in the Raw and Drawable folders do not work. For example, in my database it is implemented like this:

| Sounds | Pictures |

| R.raw.sound1 | R.drawable.picture1 |

I get the data from the database like this:

 int mSounds = cursor.getInt(0); int mPictures = cursor.getInt(1); 

And now when I try to ask for example a picture:

 imageView.setBackground(mPictures); 

The application crashes and gives an error that mPictures empty.

  • 2
    Nothing good will come of it. values ​​in R are generated, at any time you can get the base desynchronization and values ​​in R. Well, in principle, you have already received. - Yura Ivanov
  • Suggest a working alternative - McDaggen

1 answer 1

You can set the string in the database with the name of the resource, and then remove their identifiers like this:

 String imageName = "picture1" int resID = getResources().getIdentifier(imageName, "drawable", "package.name"); imageView.setImageResource(resID); 
  • Amendment. ImageView in setBackground accepts Drawable , and for setting images from resources you need setImageResource - Flippy
  • thanks, corrected - Serodv
  • For a single method, fine, but with an array problem. I have a one-to-many query, and in some positions an array of strings - McDaggen
  • But in general, yes, this is the answer to my question. - McDaggen