将位图转换为base64

时间:2016-08-24 12:19:08

标签: android image bitmap base64 encode

我想将图像上传到服务器,该服务器接受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:无法分配带有13777320个空闲字节的15521174字节分配和13MB直到OOM

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

3 个答案:

答案 0 :(得分:2)

试试这段代码:

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

答案 1 :(得分:1)

你必须得到'OutOfMemoryError',因为你可能正在使用分辨率非常高的图像并将其直接加载到内存中,因此base64的字符串太大了。始终在内存中加载图像的子采样版本以避免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; } Take a look at this. 来自文档:

  

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

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

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

然后解码采样版本:

{{1}}

加载位图的采样版本后,将位图转换为base64。

答案 2 :(得分:0)

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

试试这个