Android-将压缩位图(gzip)转换为Base64导致数据丢失
我正在尝试使用gzip将位图转换为base64 我尝试了该解决方案,但我发现此错误GZIP无法解决或不是字段 我下面的解决方案是可行的,但图像在底部被剪切 这是我的代码:Android-将压缩位图(gzip)转换为Base64导致数据丢失,android,bitmap,base64,gzip,Android,Bitmap,Base64,Gzip,我正在尝试使用gzip将位图转换为base64 我尝试了该解决方案,但我发现此错误GZIP无法解决或不是字段 我下面的解决方案是可行的,但图像在底部被剪切 这是我的代码: Bitmap myBitmap = BitmapFactory.decodeFile("\path\to\file.jpg"); ByteArrayOutputStream stream=new ByteArrayOutputStream(); GZIPOutputStream
Bitmap myBitmap = BitmapFactory.decodeFile("\path\to\file.jpg");
ByteArrayOutputStream stream=new ByteArrayOutputStream();
GZIPOutputStream gzipOstream=null;
try {
gzipOstream=new GZIPOutputStream(stream);
} catch (IOException e) {
e.printStackTrace();
}
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, gzipOstream);
byte[] byteArry=stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArry, Base64.NO_WRAP);
try {
gzipOstream.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
此代码是否会使图像丢失数据,或者它是服务器端的某个东西?调用
mBitmap.compress()后代码>调用gzipOstream.flush()代码>这保证输出字节流包含所有内容。然后您的toByteArray()代码>将获取当前缺少的数据
试试这个:
Bitmap myBitmap = BitmapFactory.decodeFile("\path\to\file.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
GZIPOutputStream gzipOstream = null;
try {
gzipOstream = new GZIPOutputStream(stream);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, gzipOstream);
gzipOstream.flush();
} finally {
gzipOstream.close();
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
stream = null;
}
if(stream != null) {
byte[] byteArry=stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArry, Base64.NO_WRAP);
// do something with encodedImage
}
调用mBitmap.compress()后代码>调用gzipOstream.flush()代码>这保证输出字节流包含所有内容。然后您的toByteArray()代码>将获取当前缺少的数据
试试这个:
Bitmap myBitmap = BitmapFactory.decodeFile("\path\to\file.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
GZIPOutputStream gzipOstream = null;
try {
gzipOstream = new GZIPOutputStream(stream);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, gzipOstream);
gzipOstream.flush();
} finally {
gzipOstream.close();
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
stream = null;
}
if(stream != null) {
byte[] byteArry=stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArry, Base64.NO_WRAP);
// do something with encodedImage
}
调用mBitmap.compress()后代码>调用gzipOstream.flush()代码>这保证输出字节流包含所有内容。然后您的toByteArray()代码>将获取当前缺少的数据
试试这个:
Bitmap myBitmap = BitmapFactory.decodeFile("\path\to\file.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
GZIPOutputStream gzipOstream = null;
try {
gzipOstream = new GZIPOutputStream(stream);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, gzipOstream);
gzipOstream.flush();
} finally {
gzipOstream.close();
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
stream = null;
}
if(stream != null) {
byte[] byteArry=stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArry, Base64.NO_WRAP);
// do something with encodedImage
}
调用mBitmap.compress()后代码>调用gzipOstream.flush()代码>这保证输出字节流包含所有内容。然后您的toByteArray()代码>将获取当前缺少的数据
试试这个:
Bitmap myBitmap = BitmapFactory.decodeFile("\path\to\file.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
GZIPOutputStream gzipOstream = null;
try {
gzipOstream = new GZIPOutputStream(stream);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, gzipOstream);
gzipOstream.flush();
} finally {
gzipOstream.close();
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
stream = null;
}
if(stream != null) {
byte[] byteArry=stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArry, Base64.NO_WRAP);
// do something with encodedImage
}
那对我不起作用。你是我的英雄:)效果很好。非常感谢,欢迎光临。感谢@TheIronMarx提供的格式设置。我批准了。编辑代码时我正在火车上;)手机很烂。我正要编辑它,你也会打败我;)那对我不起作用。你是我的英雄:)效果很好。非常感谢,欢迎光临。感谢@TheIronMarx提供的格式设置。我批准了。编辑代码时我正在火车上;)手机很烂。我正要编辑它,你也会打败我;)那对我不起作用。你是我的英雄:)效果很好。非常感谢,欢迎光临。感谢@TheIronMarx提供的格式设置。我批准了。编辑代码时我正在火车上;)手机很烂。我正要编辑它,你也会打败我;)那对我不起作用。你是我的英雄:)效果很好。非常感谢,欢迎光临。感谢@TheIronMarx提供的格式设置。我批准了。编辑代码时我正在火车上;)手机很烂。我正要编辑它,你也会打败我;)