1

我想从图库中选择一个图像并将其转换为 base64 字符串以插入数据库。但是这个字符串有空字符并且非常非常长。当我在 asp.net 中执行相同的过程时,解码的字符串并没有那么长,并且没有空字符。由于返回的字符串很长,我收到错误。我怎样才能做到这一点 ?

Bitmap bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
byte[] b = baos.toByteArray(); 

imageStr = Base64.encodeToString(b, Base64.DEFAULT);

imageStr 像这样返回:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB

AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB

AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAMgAlUDASIA

AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA

AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExc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04pYfMihEx863x/Nx68Z6D/gXXk1CIvJe482e3/5h3/oTj1HTb+RHXbk8v1VfzP71/8AIkkLjysj
MH5e7dj64HPbkZORmn+5x287P2n/AMe6euM/hj3reEUMUkU37ji6H2U23Tr6/rnv0PPNZotZ4omm
86CAcfara2+rZ4x7L+mTkDNgZo877MfK/wBdx+WWz/T3z1/hqa2/dSRf89cDPUd2A/kT9M8HPI/7
25tIMwTw4/5duO7D16kgkde/UnkcTyyH99B/pHT0PL+/HQkjtwO7EgGlOiyuZvOE5GPsvX1bHG7P

.....

wxNwReVbRQ59Me+C/vxwD1PU98UAZQihi8r9z++uCMdOeT0z0HGevOepOMxRRfu5/Kmn877UP5nO
B64C5/4F0JOdJx5sbf8APaf/AJdrbHqw7j0GevQDkHOYYuh82H9907dAW9uBxgfjkkmuc9Aje1/0
eL/nt9qGe3Qn69B1/AdcGgRfu4skTw/0y368g9evBJIq4JR5TTS/v/s2PbPLjnn/AHcZ7leeHJUQ
zeXF+Hr6uPXt39yOmMnoPPOeFr5Xlfucw5GOvq+O/cevTHqTkeGGV/8AU56foW9//wBXOcgcbEX2
OIXE0sPnzW+PsuMdCXH17HA5OT1OM1TeI+ZcTfT16kv09s9O/XkkFqAA58m4zzDb47d9x7H6D8T1
4LVThin/AHUNpD5/T/tzGX9/YZ+o9VFXFi820v8AP7ib5f8Aj5z/AHj654+U57fj1BF5uZvJ8jGB
j6l+evbHTPGTyck1z1i6
4

1 回答 1

0

使用Base64.NO_WRAP标志去除换行符

压缩时使用 90 质量将大大减小输出大小。输出的长度将是字节数组长度的 4/3 倍

于 2013-06-19T12:40:46.157 回答