有人能告诉我将图像转换为base64字符串的代码。我写下面的代码没有得到正确的结果。
String filepath = "/sdcard/Image/image1.jpg";
File imagefile = new File(filepath);
FileInputStream fis = null;
try {
fis = new FileInputStream(imagefile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
结果我得到的格式不正确,以3点结束......
字节数组我以三个点结束...
[-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, -1, -37, 0, 67, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -37, 0, 67, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -64, 0, 17, 8, 1, 64, 0, -16, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, -1, -60, 0, -75, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, -127, -111, -95, 8, 35, 66, -79, -63, 21, 82, -47, -16, 36, 51, 98, 114, -126, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, -125, -124, -123, -122, -121, -120, -119, -118, -110, -109, -108, -107, -106, -105, -104, -103, -102, -94, -93, -92, -91, -90, -89, -88, -87, -86, -78, -77, -76, -75, -74, -73, -72, -71, -70, -62, -61, -60, -59, -58, -57, -56, -55, -54, -46, -45, -44, -43, -42, -41, -40, -39, -38, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -1, -60, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, -1, -60, 0, -75, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, -127, 8, 20, 66, -111, -95, -79, -63, 9, 35, 51, 82, -16, 21, 98, 114, -47, 10, 22, 36, 52, -31, 37, -15, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, -126, -125, -124, -123, -122, -121, -120, -119, -118, -110, -109, -108, -107, -106, -105, -104, -103, -102, -94, -93, -92, -91, -90, -89, -88, -87, -86, -78, -77, -76, -75, -74, -73, -72, -71, -70, -62, -61, -60, -59, -58, -57, -56, -55, -54, -46, -45, -44, -43, -42, -41, -40, -39, -38, -30, -29, -28, -27, -26, -25, -24, -23, -22, -14, -13, -12, -11, -10, -9, -8, -7, -6, -1, -38, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, -2, 55, -96, -76, 100, 41, 28, -111, -96, 46, -119, 50, 16, -79, -78, 60, 82, 2, -24, -24, -56, 28, 72, -82, -84, 14, -32, 91, 57, 57, 32, -115, -75, -83, 109, 102, -84, 91, 116, 104, 62, 98, 48, 85, 114, 112, 100, 3, -8, -77, -43, 65, 35, 29, 54, -110, 9, 14, 79, 23, -16, -9, -58, -87, -31, -23, -51, -114, -79, 4, 87, -102, 52, -78, 42, -77, 77, 2, 75, 62, -98, 36, 117, 13, 53, -69, -19, 50, -120, -14, -101, -27, -122, 51, -109, -110, -15, 41, 125, -55, 39, -40, -98, 23, -45, -4, 55, -30, 27, 8, -38, -34, 11, 107, -44, 107, -107, 72, -18, 34, 75, 89, 101, 116, 36, 75, 27, -106, 80, -53, 30, -27, 32, 49, 92, -122, 80, -39, -52, -65, 53, 124, 116, -16, 110, 28, -51, -22, -106, -51, 39, 103, -85, 87, -14, -8, 123, -11, 119, 109, -92, 121, -76, 49, -111, 106, -17, 118, -105, -90, -22, -18, -51, -89, -25, 109, -10, -46, -41, 103, -126, 91, -38, -94, -85, 110, -120, 12, 54, 7, -55, -114, -101, -3, -119, 3, 104, 28, -25, -44, 2, 48, -39, -47, -122, -51, 112, -37, 17, 71, 0, 16, 20, 1, -116, -65, 25, 62, -92, 100, -109, -58, 119, 100, -28, -28, -3, -109, 101, -32, 111, 14, 61, -66, 95, 65, -79, -109, -26, 84, 57, -75, -116, 56, -35, 51, -122, 98, 66, 70, 66, -87, -25, 110, 114, -87, -72, -86, 2, 49, 93, 61, -81, -61, 31, 8, 58, 21, 109, 2, -60, -107, -57, 43, 27, 12, -106, 103, 39, 44, 8, 99, -128, 120, 29, 66, -32, 12, -107, -7, -79, 88, 121, 61, -91, 123, 37, -41, -94, 106, -35, 63, -90, -41, 84, -17, -46, -79, 84, -19, -83, -42, -35, 63, -59, -25, -27, -75, -5, -85, -67, 89, -7, -17, -81, -8, 70, -49, 94, -77, 49, 58, 8, 110, -112, 31, -78, 93, 32, 27, -30, -109, 46, 118, -66, 6, 100, -115, -103, 70, -12, 39, 118, 55, 0, -63, -64, 39, -61, 111, 52, -21, -35, 30, -12, 89, 95, 90, 70, -109, 90, 72, -82, 85, -30, 30, 85, -60, 32, -66, 26, 55, 0, 52, -80, -54, -96, 16, -63, -125, 114, 87, 34, 84, 106, -3, -109, -75, -8, 57, -32, -126, -30, 71, -48, 109, -9, 22, 24, 6, -25, 80, 84, 57, 50, 19, -14, 37, -54, -115, -72, 24, 94, 2, -125, -114, 114, -96, -41, 61, -29, -113, -39, -97, -31, -1, 0, -116, 116, 59, -69, 59, 27, 67, -93, -21, 17, -62, 100, -46, -75, 40, -26, -71, -105, -20, 119, 96, 57, -37, 44, 18, -36, 58, -49, 103, 113, -64, -70, -124, -31, -54, -108, 120, -27, 73, 86, 22, -81, 71, 11, 57, -46, -117, -89, 55, 120, -69, 90, -41, -70, -42, 87, -66, -119, 90, -38, -37, 93, -35, -84, -73, -31, -59, 58, 53, -105, 52, 18, -25, -70, -43, -11, -43, 119, -12, -35, 121, 95, 85, -81, -29, -35, -18, -98, 86, 89, -54, 36, 41, 21, -68, 74, -32, -85, -57, 25, 49, -76, -125, 110, 16, -112, -46, 76, 60, -59, 14, -86, -90, 77, -120, -52, 81, -43, 36, -112, -25, 27, 72, -38, 41, -92, 18, 29, -31, -45, 108, 126, 81, 6, 72, -56, -108, -68, -103, 14, 66, -104, -10, -95, -37, -50, 67, -71, -36, 54, 18, 125, -109, -57, 30, 13, -15, 47, -125, 124, 67, 123, -31, -49, 24, 88, -83, -83, -12, 46, -78, 69, 40, -116, 125, -98, -14, -38, 77, -48, -61, -88, 88, 79, 18, -60, 39, -79, -106, 52, -52, 126, 90, -97, 46, 85, -110, -34, 72, -31, -71, 91, -104, -85, -128, -109, 79, -98, -56, -83, -62, 5, 56, -108, -7, 44, 85, 27, -26, -122, 70, -36, 93, 28, -78, -100, 48, 25, 87, -32, -126, 87, 44, -124, -77, 122, 42, 55, -115, -42, -82, -17, -81, 75, -7, -11, -74, -66, 123, 110, 121, -100, -42, -109, -117, -46, -38, 126, -114, -9, 122, 126, 127, 61, 78, 42, 61, -88, -50, 48, 50, -96, -20, -32, 16, -60, 49, -57, 94, -128, -125, -44, 100, -6, -100, 19, 90, -38, 124, -78, -38, -50, -109, -37, 22, 89, -61, -58, -54, -15, 41, 51, 44, -118, -25, 97, -117, -111, -110, 27, -26, 10, 78, 9, 37, 65, -36, 55, 30, -81, -5, 4, -21, 17, 92, -21, 49, 62, -97, 98, -86, -115, 53, -22, 8, -38, -34, 8, -89, 15, 34, -104, 45, -31, 70, -107, -53, -56, -116, -73, 9, 20, 106, 124, -46, -14, 45, -72, 103, -115, -31, 94, 102, 21, -114, 53, -99, -36, -54, -78, -57, -27, -76, 5, 2, -107, 47, -26, 48, 62, 97, 103, 13, 24, -40, 55, 35, 40, 98, 31, 106, -107, 1, -117, -44, 90, -21, -110, -41, -69, -73, -101, -69, 106, -33, -7, 43, -13, -13, -22, -9, -91, 86, 84, -99, -42, -85, 71, 111, -101, 77, -4, -41, 79, 61, 118, 76, -6, 83, -64, -2, 38, -121, 92, -119, 109, 47, -46, 40, 53, 63, 37, 89, 11, 5, 72, -75, 8, -47, -103, 90, 107, 115, -48, -56, -90, 51, -10, -120, 65, 37, 25, 89, -108, 21, -13, 21, 125, 122, -46, -34, 33, -100, -94, -87, -62, 14, 80, 99, -17, 74, 7, 108, -29, -100, 12, -14, 73, 61, -41, 39, -31, -51, 47, 81, -72, -73, -106, 48, 37, -100, 71, 4, -53, 50, 8, -27, 43, 44, 50, -110, 51, 37, -69, -100, -108, 114, 85, 11, 1, -14, -74, 62, 98, 88, 43, 15, -45, 127, -39, -105, 79, -8, 95, -15, 94, 59, 127, 11, -8, -53, 82, -41, 52, 47, 24, -55, 10, -49, -90, -51, 5, -10, -101, 5, -121, -120, -83, 84, 72, -57, -20, 11, 119, -89, -54, 63, -76, 97, 69, 111, -76, -39, 2, -19, 49, 13, 61, -85, 21, 55, 86, -15, -8, 88, -84, -79, -58, 115, -100, 91, -77, 122, -57, -86, -43, -66, -49, 116, -81, -9, 106, -37, -69, -6, 124, 6, 105, 79, -39, -72, -44, 87, -27, 105, 38, -76, 111, -43, 53, -39, 59, -83, -43, -45, -69, 91, -16, -55, -90, -58, -56, -89, -55, 66, 64, 93, -89, 106, -112, 126, -10, 0, 59, 112, 71, 10, 121, -25, 56, 57, -56, 10, 121, 127, 23, 124, 58, -80, -15, 85, -117, 43, 69, 21, -75, -12, 104, 90, -38, -19, 33, 92, -85, -110, -33, -70, -103, 84, 102, 72, 36, 35, 59, 9, -7, 10, -121, 83, -71, 73, -81, -43, 24, 127, 100, 79, 6, -49, 111, -74, -45, -60, 62, 35, -126, 77, -111, -76, 111, 51, 105, -110, -123, 37, 100, -36, -78, 68, -70, 117, -66, -16, 73, 12, 0, 117, 117, 1, -108, -106, 37, -98, -81, -89, -20, 113, -31, -13, 17, -14, -68, 75, -84, 76, -25, -27, -115, -28, -114, -59, 99, 12, 60, -44, 27, -31, 88, 67, 48, -32, 49, 38, 85, -34, 89, -41, 112, 35, 121, -32, -93, 71, 19, 66, -85, -87, 7, -42, 43, -15, 94, 111, 126, -34, -73, 76, -17, -87, -104, 96, -22, -46, -107, 41, -87, 114, -55, 89, -69, 59, -17, 117, 107, 54, -109, 77, 43, 125, -51, -37, -103, -53, -7, -16, -43, 124, 55, 125, -94, -34, -36, 105, -6, -115, -89, -109, 52, 7, 105, 44, -124, -84, -86, 90, 93, -78, -62, -37, 113, 36, 114, 42, -18, 12, 56, 25, 32, -31, -127, 90, -65, -31, -35, 86, -21, -61, -9, 73, 52, 39, -52, 70, 32, 50, -80, -54, 58, -125, -13, 71, 32, 99, -62, -28, 3, 25, 31, 50, -98, -8, -36, 91, -9, 23, -57, 63, -80, 47, -122, -4, 93, -91, -49, 108, 60, 85, -88, -39, 107, 16, 71, 112, -38, 109, -11, -58, -107, 103, 36, 81, 92, 108, -109, -55, 91, -119, 32, -103, 100, -106, -51, -91, 8, 46, 4, 100, -4, -123, -34, 21, 18, -18, -49, -29, -49, -60, -81, -123, -34, 45, -8, 83, -30, -99, 83, -63, -34, 50, -46, 31, 77, -43, 44, 37, 64, 80, -30, 75, 107, -69, 103, 51, 8, 117, 13, 58, -17, -124, -68, -80, -69, 68, 15, 5, -60, 120, 5, -119, -118, 84, 75, -120, -26, -116, 125, 69, 26, -85, 23, 67, -39, -44, 75, -98, -47, 82, -113, 71, 119, 56, -20, -45, -20, -92, -99, -19, -75, -35, -36, 91, -7, 106, -109, 88, 90, -41, -94, -17, 11, -90, -98, -87, -39, 59, -23, -82, -6, 95, -43, 110, -9, 61, -105, -61, -102, -123, -122, -75, 101, 29, -35, -80, 67, -57, -17, -94, 1, 75, -60, -37, -100, 20, 113, -116, -16, -33, 50, -100, 96, -85, 100, -74, 0, -49, 101, 22, -97, 1, 37, -74, 38, 24, 115, -75, 87, 29, 91, -87, 11, -23, -44, 114, 121, 25, -28, 12, -4, -103, -31, -97, 17, -35, -8, 126, -11, 39, -123, -63, -119, 54, -58, -47, -53, -111, 28, -111, 101, -127, -118, 80, -93, -25, 80, 71, -53, 38, 12, -120, 74, -112, -27, 70, 43, -11, 7, -31, 7, -63, -115, 59, -29, 55, -125, -94, -15, 63, -124, 124, 111, -90, 77, -121, -14, 117, 93, 26, 93, 54, 84, -43, 52, 107, -35, -68, 90, 94, -94, -33, -70, -122, 108, 9, 109, -26, 5, -83, -18, -96, 127, 58, 9, 100, 10, -32, 124, -50, 55, 45, -85, 66, -92, -1...
和base64字符串im也以3个点结束,这是不正确的
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA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sQIQqkUwuIgRvV9s6bgy+YjMELM6ZVJHZl3HsfCetz6LOZYoo51kA2+abiNoJo5N8V1bzW8sM0U9u+5omEvlhyDJHIwTFU2MOrwCG3VYJYgzBp2OWcKcxeYXEbK4KKszKGEiRoyIJBWfLY3emyLH...
但是当我从我的drawable资源转换图像时,我得到了正确的base64字符串
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String testa = Base64.encodeBytes(byteArray);
任何好友都可以告诉我我在上面的代码中做了什么事先谢谢
答案 0 :(得分:2)
试试这个。
File imageFile = new File("/sdcard/gallery_photo_4.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);
答案 1 :(得分:1)
我敢说最后的点只是占位符,用于显示值太长,显示/打印输出为字节数组。您的base64编码代码没有任何问题,但为什么要录制JPEG?只需在字节数组中读取文件(不是一次吞下,而是分块,我会选择缓冲区大小为4的倍数),即时转换并将其写入输出流。
PS:文件在android中读取是不好的做法,因为内存非常有限 - 你的性能会很差。
以下是示例代码,但我懒得编译或调试它:
while(( size = inputStream.read(inputBuffer)) != -1 ) {
// arraycopy only if necessary
outBuf = Base64.encodeBytes(System.arrayCopy(inputBuffer...))
outputStream.write(outBuf);
}
这不是最优化的解决方案,因为arraycopy可能会复制数据(但这必须在最后一个块上完成)
答案 2 :(得分:0)
// uris[] - the file uris array
if (uris != null && !uris.isEmpty()) {
for (int i = 0; i < uris.size(); i++) {
// current image's uri
// Uri u = Uri.fromFile(filepath) - YOUR situation
Uri u = uris.get(i);
if (u != null) {
System.gc();
InputStream is = null;
byte[] data = null;
try {
is = mContentResolver.openInputStream(u);
// converting to byteArray / my Procedure
data = getFileBytes(is);
} catch (Exception ex) {
ex.printStackTrace();
}
u = null;
// converting bytes to Base64
String data_string =
Base64.encodeToString(data, Base64.URL_SAFE);
// my pairs for transfer, may not use
if( ! data_string.trim().equals("")){
nameValuePairs.add(
new BasicNameValuePair(
"data[file_" + i + "]",
data_string
)
);
}
data_string = null;
}
}
}
/* convert InputStream to Byte array procedure */
public byte[] getFileBytes(InputStream ios) throws IOException {
ByteArrayOutputStream ous = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
int read = 0;
while ((read = ios.read(buffer)) != -1)
ous.write(buffer, 0, read);
} finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ios != null)
ios.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return ous.toByteArray();
}
还要检查你在哪里看到base64。如果调试 - 它将被删除!
确定。尝试使用我的代码将图像解码为您想要的MPx。这是一百万像素:
是:强>
minimizeBitmap(filePath, 1000000, true)
我的代码:
public Bitmap minimizeBitmap(String aPath, int aMps, boolean aSaveOrNot) throws FileNotFoundException {
Uri aUri = null;
{
File f = new File(aPath);
aUri = Uri.fromFile(f);
f = null;
System.gc();
}
InputStream in = mContentResolver.openInputStream(aUri);
try {
final int IMAGE_MAX_SIZE = aMps;
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Bitmap b = null;
in = mContentResolver.openInputStream(aUri);
aUri = null;
if (scale > 1) {
scale--;
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
int height = b.getHeight();
int width = b.getWidth();
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;
if (aSaveOrNot) {
FileOutputStream out = new FileOutputStream(_fileName);
b.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.close();
}
System.gc();
return b;
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
//
// Log.i("junr", "out getBitmap");
// Log.i("junr", String.valueOf(Environment.getExternalStorageDirectory()));
if (aSaveOrNot) {
FileOutputStream out = new FileOutputStream(_fileName);
b.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.close();
}
return b;
} catch (IOException e) {
Log.e("junr", e.getMessage(),e);
return null;
}
}