1
我從圖庫中選擇圖片,轉換成Base64併發送到服務器。對於JPEG圖像,它工作正常;我從服務器上的相冊上傳的圖像也會顯示在服務器文件夾中。但是,當我從手機圖庫上傳PNG格式圖像時,它不會在服務器上顯示相同的圖像;相反,它會在其周圍形成黑色邊緣。我真的不知道發生了什麼問題?爲什麼PNG在圖像周圍形成PNG格式的黑邊?
另外,我的實際圖像與給定的JPEG圖像相同。
參考圖片:
JPEG:
PNG:
我只是想擺脫它不應該出現在PNG格式的圖片黑色邊框的。
下面是我的代碼片段
FileInputStream mFileInputStream = null;
try {
mFileInputStream = new FileInputStream(imagePathFromSDCard);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
Bitmap bitmap = safeImageProcessing.decodeFile(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] ba = bos.toByteArray();
String encodedImage = Base64.encodeToString(ba, Base64.NO_WRAP);
//this line sends image base64 to server & there i decode into original
new ImageAsync().sendImageProcess(getActivity(), encodedImage, this);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我認爲它會崩潰,如果我直接將文件轉換爲base64並得到OutOfMemory異常。 – VVB
不會。這將明確使用較少的內存。因爲它佔據了最多的位圖。 – greenapps
但是,如果您希望儘可能少地使用內存,那麼不要以base64編碼開始。因此,您不必首先在memorny中的字節數組中加載文件,但可以直接上載文件。 – greenapps