将位图转换为 base64 时得到一半图像
Getting half image when convert bitmap to base64
我想将图片上传到接受 base64 编码格式图片的服务器。我使用这段代码对位图进行编码,但它只对图像的一半进行编码,服务器只接收图像部分的一半。
这是我的代码。
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
// Send to server(encoded)
当我尝试对图库中的图像进行编码时,它弹出一个错误并使应用程序崩溃,并出现错误。
java.lang.OutOfMemoryError:无法分配 15521174 字节的分配,其中有 13777320 个空闲字节和 13MB,直到 OOM
我想对完整图像进行编码,有什么帮助吗?
试试这个代码:
BitmapFactory.Options options;
options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 6;
Bitmap bitmap1 = BitmapFactory.decodeFile(fileUri1.getPath(),
options);
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
if(bitmap1 != null) {
bitmap1.compress(Bitmap.CompressFormat.PNG, 10, baos1);
byte[] b1 = baos1.toByteArray();
bitmapstring1 = Base64.encodeToString(b1,
Base64.DEFAULT);
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
Log.d(TAG, "getStringImage: "+encodedImage.trim());
return encodedImage;
}
试试这个
你一定得到 'OutOfMemoryError' 因为你可能正在使用非常高分辨率的图像并将其直接加载到内存中,因此 base64 的字符串太大。始终在内存中加载图像的子采样版本以避免 OutOfMemoryError
。 Take a look at this.
来自文档:
For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
首先计算图像的样本大小:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
然后解码采样版本:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
加载位图的采样版本后,将位图转换为base64。
我想将图片上传到接受 base64 编码格式图片的服务器。我使用这段代码对位图进行编码,但它只对图像的一半进行编码,服务器只接收图像部分的一半。
这是我的代码。
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
// Send to server(encoded)
当我尝试对图库中的图像进行编码时,它弹出一个错误并使应用程序崩溃,并出现错误。
java.lang.OutOfMemoryError:无法分配 15521174 字节的分配,其中有 13777320 个空闲字节和 13MB,直到 OOM
我想对完整图像进行编码,有什么帮助吗?
试试这个代码:
BitmapFactory.Options options;
options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 6;
Bitmap bitmap1 = BitmapFactory.decodeFile(fileUri1.getPath(),
options);
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
if(bitmap1 != null) {
bitmap1.compress(Bitmap.CompressFormat.PNG, 10, baos1);
byte[] b1 = baos1.toByteArray();
bitmapstring1 = Base64.encodeToString(b1,
Base64.DEFAULT);
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
Log.d(TAG, "getStringImage: "+encodedImage.trim());
return encodedImage;
}
试试这个
你一定得到 'OutOfMemoryError' 因为你可能正在使用非常高分辨率的图像并将其直接加载到内存中,因此 base64 的字符串太大。始终在内存中加载图像的子采样版本以避免 OutOfMemoryError
。 Take a look at this.
来自文档:
For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
首先计算图像的样本大小:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
然后解码采样版本:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
加载位图的采样版本后,将位图转换为base64。