0

I'm reading Android's Camera API documentation about camera API, this is my first ever use of the API. I came accross to some code lines where integers MEDIA_TYPE_IMAGE is being invoked, one of them are:

      // create a file to save the image
      fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

Does this mean like, 1 represents type of file that the camera produces, which is an image ? Or this just represents like true for activing the camera?

1 Answers1

1

1 in this example is a custom value for a variable called MEDIA_TYPE_IMAGE. It is stored in a variable so that instead of seeing 1's everywhere, you see the name of the variable, which has meaning. 1 has no meaning.

Somewhere in the example it is used to be able to distinguish types

if (type == MEDIA_TYPE_IMAGE){
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "VID_"+ timeStamp + ".mp4");
}

Now imagine instead of that, it would say this

if (type == 1){
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "IMG_"+ timeStamp + ".jpg");
} else if(type == 2) {
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "VID_"+ timeStamp + ".mp4");
}

that is confusing. Confusing programmers is asking for trouble.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • @Hey-men-whatsup it will do exactly the same, but it's better to use a variable for it. Also read this http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad – Tim Jul 26 '16 at 13:28
  • I see, i missed the second part of your answer. Thank you very much! – Plain_Dude_Sleeping_Alone Jul 26 '16 at 13:29