2

So I'm learning android dev, and I'm building a simple app to do so.

The app is meant to display an overview of 26 buttons (one for each letter of the alphabet); upon clicking any of these buttons, a new activity should launch and display a corresponding image (a picture of an apple, for instance, if "A" is clicked).

I'm trying to use putExtras to put the filename (and getting it in the new activity), as below for each letter.

Button buttonA = (Button) findViewById(R.id.buttonA);
        buttonA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startLetter = new Intent(getApplicationContext(), LetterPage.class);
                startLetter.putExtra("filename","Slide01.gif");
                startActivity(startLetter);

And then I'm trying to get the filename:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_letter_page);

        String displayFile;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                displayFile = null;
            } else {
                displayFile = extras.getString("filename");

            }
        } else {
            displayFile = (String) savedInstanceState.getSerializable("filename");
        }
ImageView imgview = (ImageView) findViewById(R.id.imageView);
                imgview.setImageResource(R.drawable.[stuck on what to put here]);

In the above, I get the option to select the file from a drop-down menu, but how would I put it as R.drawable.[filename]?

I could, I think, just have a large if-else-if-else block that checks if filename.equals("SlideA") and then just R.drawable.SlideA like that, but is there not a better way?

Bonus question: how would I get the image from the phone's DCIM directory (on the phone, not SD card)? [Adding this bonus just in case it's drastically different in that case, or if there's a unique method for the DCIM case].

Thanks!

Paul12596
  • 300
  • 3
  • 13
  • in which location Slide01.gif is saved ? – Pawan Singh Chauhan Oct 04 '17 at 08:44
  • try this https://stackoverflow.com/a/9156893/5746918 – Ishita Sinha Oct 04 '17 at 08:48
  • @PawanSinghChauhan Currently, in res/drawable. I'm hoping to change it to read the images from root/Phone/DCIM/alphabetImages/Slide01 etc (roughly speaking). – Paul12596 Oct 04 '17 at 08:53
  • just pass drawable name in bundle and after getting name set it like if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { imageView.setImageDrawable(getApplicationContext().getDrawable(R.drawable.name_here)); } else { imageView.setImageDrawable(getResources().getDrawable(R.drawable.name_her)); } – Pawan Singh Chauhan Oct 04 '17 at 08:58
  • imgview.setImageResource(R.drawable. **Slide01** ); –  Oct 04 '17 at 09:37

0 Answers0