Android 低尺寸图像上传
我尝试上传图像到服务器。我通过路径获取图像并将其转换为字节数组:Android 低尺寸图像上传,android,android-volley,android-image,android-bitmap,android-file,Android,Android Volley,Android Image,Android Bitmap,Android File,我尝试上传图像到服务器。我通过路径获取图像并将其转换为字节数组: BitmapFactory.Options bmOptions = new BitmapFactory.Options(); Bitmap picture = BitmapFactory.decodeFile(imagePath, bmOptions); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); ByteArrayOutputStream
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
如何在不增加大小的情况下发送图像(按路径选择)
解决方案 @没错。我已将图像转换为文件:
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;
}
公共静态字节[]fullyReadFileToBytes(文件f)引发IOException{
int size=(int)f.length();
字节字节[]=新字节[大小];
字节tmpBuff[]=新字节[大小];
FileInputStream fis=新的FileInputStream(f);;
试一试{
int read=fis.read(字节,0,大小);
如果(读取<大小){
int剩余=大小-读取;
而(保持>0){
read=fis.read(tmpBuff,0,剩余);
数组复制(tmpBuff,0,字节,大小-保留,读取);
保持-=读取;
}
}
}捕获(IOE异常){
投掷e;
}最后{
fis.close();
}
返回字节;
}
将图像文件放在字节数组中而不使用中间位图。是否尝试以64位字符串编码发送图像?将图像文件放在字节数组中而不使用中间位图。@greenapps您可以添加此注释作为答案。