Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Sending Messages Adding a File to the Message: Part 2

Joseph Karanja
Joseph Karanja
169 Points

Reducing image size

Hi all.

I am currently going through the lessons and adapting the code for my own separate project which requires uploading photos only. When i tested the upload bit, all went fine, but on looking at the uploaded details in parse, i saw one photo (picked from gallery) was 3mb.

To put this into perspective, an image in my phone gallery is 2.6MB. The same image after uploading to parse has actually increased in size and is now 3.16MB.

That's a deal breaker right there. Is there a way to change the code in the helper classes to reduce image size to be uploaded to a fixed value, say 500kb?

The link for the helper classes is http://treehouse-code-samples.s3.amazonaws.com/Android/helper_files.zip

Thanks for reading and looking forward to your replies

3 Answers

Harry James
Harry James
14,780 Points

Hey Joseph!

Sorry about my late response - I've been extremely busy with exams lately.


Anyhow, I don't believe it's possible to always have a fixed file size for images - they will always differ by a few kilobytes/bytes and it's extremely difficult to set a file to be an exact size.

I haven't actually noticed an increase in file size as my images are set to decrease in file quality before being sent off (Which Ben does show you how to do in the course if you're interested). I did however, test my code out without the decrease in file size and found that my 5.08mb file remained at 5.08mb so I'm not sure what caused your photo to end up with a larger size.

Perhaps your device is telling you an incorrect file size? That would be my only guess. I know this works in Lollipop definitely but not sure about other versions: when you click Choose Photo, go into the Overflow Menu and select Show file sizes and see what it comes up with there - I got the same file size shown on this screen as I did after going into Parse and downloading the image from my Dashboard. See if this works for you.


Hopefully this should help but if there's anything else you want to know, give me a shout and I'll try my best to help out :)

Joseph Karanja
Joseph Karanja
169 Points

Thanks for replying James. I did as you asked and the uploaded images are increasing in size. I tested with a 1.26mb file from my phone, uploaded it to parse, downloaded it from my dashboard, and now it's gone up in size to 3.69mb. I'm thinking maybe there's a problem with my helper files, as i had said earlier, i removed some code to adapt it to for my app. I'll compare the original with what i have and see how to move on from there

Marcelino Yax
Marcelino Yax
10,966 Points

I am working with images and found the same problem. I uploaded an image of 2.49MB and got an image of 3.19MB from Parse. It happens because of different format: the FileHelper class compresses the JPEG image file from gallery to PNG file: bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream). Google the difference between JPEG and PNG to learn more. If you want to reduce it down, do this: bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream), but the quality of your image comes down.

Harry James
Harry James
14,780 Points

Hey there!

Nice find Marcelino Yax! We can even swap out the format based on the extension type.

To do this, get the extension type in RecipientsActivity.java after you have set mMediaUri like this:

// First, add this member variable:
private String mExtension;
// Next, set the extension:
mExtension = FilenameUtils.getExtension(mMediaUri.toString());

Now, update your reduceImageForUpload() method in FileHelper.java to take in an extension as well:

public static byte[] reduceImageForUpload(byte[] imageData, String ext) {

Back over in RecipientsActivity.java, you will need to update the method parameters:

fileBytes = FileHelper.reduceImageForUpload(fileBytes, mExtension);

Finally in FileHelper.java again, we can compress based on the file type:

if (ext.equals("jpg") || ext.equals("jpeg")) {
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
} else {
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
}

Phewp! That was a lot of file switching, sorry about that! This will work perfectly fine as a way to compress based on file type. Note that I am only compressing JPG's separately here, if it's of a different format then it will default to a PNG. Also, my code could probably have a bit of polishing here! I did originally try for a switch statement but realised Android uses Java 6 and String switches came in Java 7 :'(

If there's anything else you need, feel free to give me a shout :)

Marcelino Yax
Marcelino Yax
10,966 Points

Great job! I understand it.