2016-08-24 129 views
0

我想將圖像上傳到服務器,該圖像以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:無法分配與13777320個免費字節一個字節15521174分配和13MB直到OOM

我想編碼完整的圖像,任何幫助嗎?

+1

將其作爲流發送。而不是將其存儲在字節數組中。當你內存不足時。 – Doomsknight

+1

谷歌搜索'OutOfMemoryError' –

+1

@KZoNE試試我的答案 –

回答

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

感謝您的答案,但我覺得這會降低圖像的質量。其實我不想讓它質量低下?你知道任何可以降低質量的代碼嗎? 我的意思是,我認爲更好地使用bitmap1.compress(Bitmap.CompressFormat.PNG,100,baos1); 你覺得呢? – KZoNE

+1

使用它bitmap1.compress(Bitmap.CompressFormat。PNG,100,baos1);沒有問題發生@KZoNE –

+0

謝謝我解決了這個問題@Damini – KZoNE

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

試試這個

+1

這個答案與他發佈的代碼不同(除了壓縮程度較低) – Doomsknight

+0

發佈我的代碼之前我沒有看到該答案 – UserSharma

+0

我的意思是代碼題。 :)另一個答案是不同的 – Doomsknight

1

你必須得「的OutOfMemoryError」,因爲可使用的是非常高的分辨率的圖像,並直接加載到內存,因此base64的String太大。總是在內存中加載一個子採樣版本的圖像,以避免OutOfMemoryErrorTake 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 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); 
} 

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

然後解碼採樣版本加載,然後將位圖轉換爲base64。

+0

我已經用戶您的示例,但仍然有一個白色的一半圖像。你知道爲什麼發生了嗎? –