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 {

    private String path;
    private String id;
    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);
        byte[] byteArray = baos.toByteArray();
        try {
        } catch (IOException e) {
        return Base64.encodeToString(byteArray, Base64.DEFAULT);

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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