Search code examples
androidandroid-volleyandroid-imageandroid-bitmapandroid-file

Low size image uploading


I try upload image to server. I get image by path and convert it to byte array:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap picture = BitmapFactory.decodeFile(imagePath, bmOptions);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ByteArrayOutputStream bao = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] bytes = bao.toByteArray();

builder.addPart("uploadedfile", new ByteArrayBody(bytes, "name" + ".jpg"));

For example image's size is 300kb but size of uploaded image is 800kb.

How can I send image (selected by path) without size increasing?


SOULUTION

@greenapps right. I converted image as file:

public static byte[] fullyReadFileToBytes(File f) throws IOException {
    int size = (int) f.length();
    byte bytes[] = new byte[size];
    byte tmpBuff[] = new byte[size];
    FileInputStream fis= new FileInputStream(f);;
    try {

        int read = fis.read(bytes, 0, size);
        if (read < size) {
            int remain = size - read;
            while (remain > 0) {
                read = fis.read(tmpBuff, 0, remain);
                System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
                remain -= read;
            }
        }
    }  catch (IOException e){
        throw e;
    } finally {
        fis.close();
    }

    return bytes;
}

Solution

  • Put the image file in a byte array without using an intermediate Bitmap.