-1
我尝试上传图像到服务器。我通过路径得到的图像并将其转换为字节数组:尺寸偏小图像上传
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"));
例如图像的大小为300KB,但上传的图片的大小为800KB。
如何在不增加尺寸的情况下发送图像(通过路径选择)?右
SOULUTION
@greenapps。我将图像转换为文件:
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;
}
您是否尝试将图像作为64位字符串编码发送? – Eenvincible
将图像文件放在字节数组中而不使用中间位图。 – greenapps
@greenapps您可以添加此评论作为答案。 –