Home » Android » android – How to upload images to server saved on SD card?-Exceptionshub

android – How to upload images to server saved on SD card?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I want to upload all images fetched from the SD card of an Android device to a server. The image array converted in JSON looks as follows:

[
  "/storage/emulated/0/DCIM/CAMERA_PIC/IMG_20200217214740.jpg",
  "/storage/emulated/0/DCIM/CAMERA_PIC/IMG_20200217215427.jpg",
  "/storage/emulated/0/DCIM/CAMERA_PIC/IMG_20200217222919.jpg",
  "/storage/emulated/0/DCIM/CAMERA_PIC/IMG_20200218102731.jpg"
]

How can I do this in Java?

How to&Answers:

The brief answer: It depends (on the backend at the server site and your programming language).

Assuming that you are using JAVA, I found e.g. a tutorial on Downloading & Uploading Images.

We use Java class ByteArrayOutputStream, which can be found under java.io package. […]

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);

In order to convert the image to byte array, we use toByteArray() method of ByteArrayOutputStream class: […]

byte[] bytes = baos.toByteArray();