Writing to SD card on Android

I am writing a simple Android app to compress my photos to save space. I’ve found that by re-saving the JPEG with a quality of around 87 there’s, on average, a 50% saving in file size. Most importantly, I have yet to be able to tell the difference between the original and compressed photos.

I used to do this on the computer when performing backups using jpgoptim but then I had to transfer the smaller photos back to the devices and it was all a bit messy because I couldn’t find an easy way to script getting access to the internal and SD card via USB on Windows.

The main solution comes from this Stack Overflow post: https://stackoverflow.com/a/25612858/3786869

I tried many searches to get to an answer:

  • Android Oreo access SD card path
  • find location of removable SD card
  • how to read JPG photos on the DCIM Camera folder on SD card Samsung note 8
  • AccessDeniedException: /storage/emulated/0
  • permission denied when writing files to SD card

However there was not a clear solution.

I’ve yet to implement it, but I will update this post (or write a new one) when I have with a nice clean solution and some example code!