【问题标题】:Getting half image when convert bitmap to base64将位图转换为base64时获取半图像
【发布时间】:2016-08-24 12:19:08
【问题描述】:

我想将图像上传到接受 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

我想对完整图像进行编码,有什么帮助吗?

【问题讨论】:

  • 以流的形式发送。而不是将其存储在字节数组中。因为您的内存不足。
  • 在谷歌上搜索OutOfMemoryError
  • @KZoNE 试试我的答案
  • 通过多部分发送。上传图片的最佳方式
  • 感谢@Doomsknight 的回答,发送前弹出错误,当我尝试设置编码字符串时?有什么想法吗?

标签: android image bitmap base64 encode


【解决方案1】:

试试这个代码:

 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);

【讨论】:

  • 感谢您的回答,但我觉得这会降低图像质量。其实我不想让它低质量?你知道在不降低质量的情况下发送任何代码吗?我的意思是,我认为最好使用 bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos1);你怎么看?
  • 使用它 bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos1);没有问题发生@KZoNE
【解决方案2】:

您一定会收到“OutOfMemoryError”,因为您可能正在使用分辨率非常高的图像并将其直接加载到内存中,因此 base64 的字符串太大。始终在内存中加载图像的子采样版本以避免OutOfMemoryErrorTake a look at this. 来自文档:

例如,如果 1024x768 像素的图像最终会在 ImageView 中以 128x96 像素的缩略图显示,则不值得将其加载到内存中。

首先计算图片的样本大小:

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。

【讨论】:

  • 我已经使用了您的样本,但仍然有一个白色的半图像。你知道为什么会这样吗?
【解决方案3】:
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;
}

试试这个

【讨论】:

  • 这个答案与他发布的代码有何不同,(除了压缩较少)
  • 我的意思是问题中的代码。 :) 另一个答案是不同的
  • 感谢您的尝试,但这是我要解决的问题
猜你喜欢
  • 2022-09-25
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2017-02-27
  • 2013-03-28
  • 2021-07-12
  • 1970-01-01
  • 2014-04-06
相关资源
最近更新 更多