Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need to upload some images to a server. This is done with a json object. The json object has 2 values, a timestamp and a Base64 encoded string of the image. I am using the jackson library and was wondering if this is a good way to upload the images. I am worried about memory consumption if I happen to upload images asyncronusly in the future. Here is is...

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Photo {

    @JsonIgnore
    private String path;
    private String id;
    @JsonProperty("ts")
    private long timestamp;

    public Photo(long timestamp, String path) {
        this.timestamp = timestamp;
        this.path = path;
    }

    public String getData() {
        Bitmap bm = BitmapFactory.decodeFile(path);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        bm.recycle();
        byte[] byteArray = baos.toByteArray();
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
}
share|improve this question
add comment

1 Answer

I'm not familiar with Android at all, so just some generic Java notes:

  1. I'd rename bm to bitmap. A little bit longer variable names are usually easier to read and maintain.

  2. The constructor should validate its input parameters. Does it make sense to call it with null or empty string as path or a less than zero timestamp? If not, check it and throw a NullPointerException or an IllegalStateException. (Effective Java, Second Edition, Item 38: Check parameters for validity)

  3. It is a bad idea to use printStackTrace() in Android exceptions.

  4. I don't know if it differs from general Java or not but closing a ByteArrayOutputStream in Java has no effect. You might be able to omit it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.