I have an API that returns the following:
"attachment": {
"original": "......"
}
This can be an image, a pdf, a word file, etc. Here are some examples:
"attachment": {
"original": "/attachment/sample_pdf_file.pdf"
}
"attachment": {
"original": "/attachment/sample_image.jpg"
}
// Etc.
My question is, how should I go about checking this and returning it to my adapter? If it's an image I want to use Picasso
to upload the image, if it's a PDF then I want to use something else to show a pdf, and so on.
My thought process is that I should make a Model
and a Util
class for this.
So it would look something like this:
Model
public class Attachment {
@SerializedName("original")
private String mOriginal;
public String getOriginal() { return mOriginal; }
}
Utility
public class AttachmentUtil {
// Code to check stuff here
}
How should I go about checking this properly?
I'd fix the API to provide such information in returned data set:
"attachment": {
"original": "/attachment/sample_pdf_file.pdf",
"original_type": "pdf"
}
"attachment": {
"original": "/attachment/sample_image.jpg",
"original_type": "jpg"
}
but if you got no control over it, it most likely should be sufficient to check file name of your original
. If so called "extension" is .pdf
then it is PDF, if it is .jpg
then it JPEG and so on. Or, cleaner way, use existing methods:
guessContentTypeFromName() or guessContentTypeFromStream(). But again - fix the API if you can.