Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

How to convert image into byte array and byte array to base64 String in android?

Can someone tell me the code to convert image into byte array and that byte array into base64 string. i write the below code not getting proper result .

  String filepath = "/sdcard/Image/ic_launcher.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);

result im getting is

 [-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, 0, 72, 0, 0, -1, -20, 4, -7, 68, 117, 99, 107, 121, 0, 1, 0, 4, 0, 0, 0, 75, 0, 2, 4, -28, 0, 0, 2, 112, 0, 68, 0, 111, 0, 110, 0, 110, 0, 97, 0, 32, 0, 83, 0, 105, 0, 109, 0, 112, 0, 115, 0, 111, 0, 110, 0, 32, 0, 117, 0, 115, 0, 101, 0, 115, 0, 32, 0, 97, 0, 32, 0, 99, 0, 111, 0, 109, 0, 112, 0, 117, 0, 116, 0, 101, 0, 114, 0, 32, 0, 116, 0, 111, 0, 32, 0, 99, 0, 104, 0, 101, 0, 99, 0, 107, 0, 32, 0, 97, 0, 32, 0, 115, 0, 105, 0, 116, 0, 101, 0, 32, 0, 116, 0, 104, 0, 97, 0, 116, 0, 32, 0, 112, 0, 97, 0, 121, 0, 115, 0, 32, 0, 104, 0, 101, 0, 114, 0, 32, 0, 109, 0, 111, 0, 110, 0, 101, 0, 121, 0, 32, 0, 116, 0, 111, 0, 32, 0, 117, 0, 112, 0, 108, 0, 111, 0, 97, 0, 100, 0, 32, 0, 118, 0, 105, 0, 100, 0, 101, 0, 111, 0, 32, 0, 99, 0, 108, 0, 105, 0, 112, 0, 115, 0, 32, 0, 111, 0, 102, 0, 32, 0, 104, 0, 101, 0, 114, 0, 32, 0, 101, 0, 97, 0, 116, 0, 105, 0, 110, 0, 103, 0, 32, 0, 97, 0, 116, 0, 32, 0, 104, 0, 101, 0, 114, 0, 32, 0, 104, 0, 111, 0, 109, 0, 101, 0, 32, 0, 105, 0, 110, 0, 32, 0, 79, 0, 108, 0, 100, 0, 32, 0, 66, 0, 114, 0, 105, 0, 100, 0, 103, 0, 101, 0, 44, 0, 32, 0, 78, 0, 101, 0, 119, 0, 32, 0, 74, 0, 101, 0, 114, 0, 115, 0, 101, 0, 121, 0, 46, 0, 46, 0, 46, 0, 68, 0, 111, 0, 110, 0, 110, 0, 97, 0, 32, 0, 83, 0, 105, 0, 109, 0, 112, 0, 115, 0, 111, 0, 110, 0, 32, 0, 117, 0, 115, 0, 101, 0, 115, 0, 32, 0, 97, 0, 32, 0, 99, 0, 111, 0, 109, 0, 112, 0, 117, 0, 116, 0, 101, 0, 114, 0, 32, 0, 116, 0, 111, 0, 32, 0, 99, 0, 104, 0, 101, 0, 99, 0, 107, 0, 32, 0, 97, 0, 32, 0, 115, 0, 105, 0, 116, 0, 101, 0, 32, 0, 116, 0, 104, 0, 97, 0, 116, 0, 32, 0, 112, 0, 97, 0, 121, 0, 115, 0, 32, 0, 104, 0, 101, 0, 114, 0, 32, 0, 109, 0, 111, 0, 110, 0, 101, 0, 121, 0, 32, 0, 116, 0, 111, 0, 32, 0, 117, 0, 112, 0, 108, 0, 111, 0, 97, 0, 100, 0, 32, 0, 118, 0, 105, 0, 100, 0, 101, 0, 111, 0, 32, 0, 99, 0, 108, 0, 105, 0, 112, 0, 115, 0, 32, 0, 111, 0, 102, 0, 32, 0, 104, 0, 101, 0, 114, 0, 32, 0, 101, 0, 97, 0, 116, 0, 105, 0, 110, 0, 103, 0, 32, 0, 97, 0, 116, 0, 32, 0, 104, 0, 101, 0, 114, 0, 32, 0, 104, 0, 111, 0, 109, 0, 101, 0, 32, 0, 105, 0, 110, 0, 32, 0, 79, 0, 108, 0, 100, 0, 32, 0, 66, 0, 114, 0, 105, 0, 100, 0, 103, 0, 101, 0, 44, 0, 32, 0, 78, 0, 101, 0, 119, 0, 32, 0, 74, 0, 101, 0, 114, 0, 115, 0, 101, 0, 121, 0, 32, 0, 74, 0, 117, 0, 110, 0, 101, 0, 32, 0, 56, 0, 44, 0, 32, 0, 50, 0, 48, 0, 49, 0, 48, 0, 46, 0, 32, 0, 83, 0, 105, 0, 109, 0, 112, 0, 115, 0, 111, 0, 110, 0, 44, 0, 32, 0, 52, 0, 50, 0, 44, 0, 32, 0, 119, 0, 104, 0, 111, 0, 32, 0, 119, 0, 101, 0, 105, 0, 103, 0, 104, 0, 115, 0, 32, 0, 109, 0, 111, 0, 114, 0, 101, 0, 32, 0, 116, 0, 104, 0, 97, 0, 110, 0, 32, 0, 54, 0, 48, 0, 48, 0, 32, 0, 112, 0, 111, 0, 117, 0, 110, 0, 100, 0, 115, 0, 32, 0, 40, 0, 50, 0, 55, 0, 50, 0, 32, 0, 107, 0, 103, 0, 41, 0, 32, 0, 97, 0, 110, 0, 100, 0, 32, 0, 97, 0, 105, 0, 109, 0, 115, 0, 32, 0, 116, 0, 111, 0, 32, 0, 114, 0, 101, 0, 97, 0, 99, 0, 104, 0, 32, 0, 49, 0, 44, 0, 48, 0, 48, 0, 48, 0, 32, 0, 112, 0, 111, 0, 117, 0, 110, 0, 100, 0, 115, 0, 32, 0, 40, 0, 52, 0, 53, 0, 53, 0, 32, 0, 107, 0, 103, 0, 41, 0, 44, 0, 32, 0, 105, 0, 115, 0, 32, 0, 119, 0, 97, 0, 103, 0, 105, 0, 110, 0, 103, 0, 32, 0, 97, 0, 32, 0, 99, 0, 97, 0, 109, 0, 112, 0, 97, 0, 105, 0, 103, 0, 110, 0, 32, 0, 116, 0, 111, 0, 32, 0, 98, 0, 101, 0, 99, 0, 111, 0, 109, 0, 101, 0, 32, 0, 116, 0, 104, 0, 101, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100, 0, 39, 0, 115, 0, 32, 0, 104, 0, 101, 0, 97, 0, 118, 0, 105, 0, 101, 0, 115, 0, 116, 0, 32, 0, 108, 0, 105, 0, 118, 0, 105, 0, 110, 0, 103, 0, 32, 0, 119, 0, 111, 0, 109, 0, 97, 0, 110, 0, 44, 0, 32, 0, 97, 0, 100, 0, 109, 0, 105, 0, 116, 0, 116, 0, 105, 0, 110, 0, 103, 0, 32, 0, 116, 0, 104, 0, 97, 0, 116, 0, 32, 0, 115, 0, 104, 0, 101, 0, 32, 0, 105, 0, 115, 0, 32, 0, 97, 0, 115, 0, 32, 0, 104, 0, 117, 0, 110, 0, 103, 0, 114, 0, 121, 0, 32, 0, 102, 0, 111, 0, 114, 0, 32, 0, 97, 0, 116, 0, 116, 0, 101, 0, 110, 0, 116, 0, 105, 0, 111, 0, 110, 0, 32, 0, 97, 0, 115, 0, 32, 0, 115, 0, 104, 0, 101, 0, 32, 0, 105, 0, 115, 0, 32, 0, 102, 0, 111, 0, 114, 0, 32, 0, 99, 0, 97, 0, 108, 0, 111, 0, 114, 0, 105, 0, 101, 0, 45, 0, 114, 0, 105, 0, 99, 0, 104, 0, 32, 0, 102, 0, 111, 0, 111, 0, 100, 0, 46, 0, 32, 0, 80, 0, 105, 0, 99, 0, 116, 0, 117, 0, 114, 0, 101, 0, 32, 0, 116, 0, 97, 0, 107, 0, 101, 0, 110, 0, 32, 0, 74, 0, 117, 0, 110, 0, 101, 0, 32, 0, 56, 0, 44, 0, 32, 0, 50, 0, 48, 0, 49, 0, 48, 0, 46, 0, 32, 0, 82, 0, 69, 0, 85, 0, 84, 0, 69, 0, 82, 0, 83, 0, 47, 0, 76, 0, 117, 0, 99, 0, 97, 0, 115, 0, 32, 0, 74, 0, 97, 0, 99, 0, 107, 0, 115, 0, 111, 0, 110, 0, 32, 0, 40, 0, 85, 0, 78, 0, 73, 0, 84, 0, 69, 0, 68, 0, 32, 0, 83, 0, 84, 0, 65, 0, 84, 0, 69, 0, 83, 0, 32, 0, 45, 0, 32, 0, 84, 0, 97, 0, 103, 0, 115, 0, 58, 0, 32, 0, 72, 0, 69, 0, 65, 0, 76, 0, 84, 0, 72, 0, 32, 0, 83, 0, 79, 0, 67, 0, 73, 0, 69, 0, 84, 0, 89, 0, 41, 0, 0, -1, -37, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, -1, -37, 0, 67, 1, 9, 9, 9, 12, 11, 12, 24, 13, 13, 24, 50, 33, 28, 33, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, -1, -64, 0, 17, 8, 1, 32, 1, -52, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 28, 0, 0, 0, 7, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -60, 0, 76, 16, 0, 1, 3, 2, 4, 3, 6, 3, 5, 4, 8, 4, 5, 2, 7, 0, 1, 2, 3, 17, 0, 4, 5, 18, 33, 49, 6, 65, 81, 19, 34, 97, 113, -127, -111, 20, 50, -95, 7, 66, -79, -63, -16, 21, 35, 82, -47, 36, 51, 98, 114, -126, -94, -31, -15, 22, 83, -62, -46, 37, 99, -110, -109, -78, 8, 115, 52, 53, 68, 85, 100, -77, -30, -1, -60, 0, 26, 1, 0, 3, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, -1, -60, 0, 45, 17, 0, 2, 2, 2, 2, 2, 1, 3, 3, 3, 5, 1, 0, 0, 0, 0, 0, 1, 2, 17, 3, 33, 18, 49, 4, 65, 81, 19, 34, 97, 113, -79, -16, 20, -111, -47, 5, 35, 50, 66, -95, -31, -1, -38, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, -21, -96, -17, 64, -11, -92, -125, 74, -98, -75, -56, 116, 7, -17, 71, -52, -46, 65, -97, 10, 85, 0, 40, 80, -102, 33, 52, 40, 1, 92, -7, 82, 78, -44, 13, 36, -21, 64, 7, 67, -42, -109, 71, 64, 7, -46, -114, -109, -45, 106, 51, 64, 2, -116, -47, 80, 39, 77, 77, 0, 42, -118, -123, 9, -96, 3, -94, -10, -95, -25, 68, 117, -38, -104, 7, 66, -118, 104, 82, 0, 80, -95, -27, 69, 76, 0, 104, -89, -91, 3, 69, 64, 7, -67, 23, -67, 9, -94, -25, 64, 3, -34, -116, -111, 73, 49, 52, 9, 20, 0, 40, -113, -42, -118, -121, -122, -76, -64, 26, -46, 105, 81, 69, 20, -128, 17, -54, -123, 8, -93, -118, 64, 20, 82, -94, -124, 82, -128, -96, 4, -88, 119, 23, -24, 40, -109, 78, 44, 119, 21, 61, 69, 36, 10, 98, 22, -102, 113, 59, 82, 6, -44, -32, -90, -124, 56, 41, 66, -110, 41, 66, -87, 0, -79, 71, 69, 70, 61, 106, -47, 33, -51, 28, -47, 80, -96, 66, -88, -26, -109, 64, 26, 0, 57, -42, -114, 105, 51, 66, 105, 5, 11, -102, 20, -112, 124, 69, 29, 0, 42, -118, 104, 10, 28, -88, 0, 81, 107, 71, 66, -99, 0, 40, 81, 80, -102, 64, 84, -125, -46, -107, 34, -112, 15, -123, 43, 88, -48, 86, 38, -95, -119, -91, -45, 114, 121, -46, -3, -88, 0, -63, -93, -102, 42, 20, 0, 9, -92, -88, -47, -103, -92, 19, -50, -128, 20, 40, 77, 36, 31, 42, 19, -42, -128, 21, 52, 116, -119, -93, -15, -96, 5, 80, 59, 122, -47, 10, 7, 97, -74, -30, -128, 21, 52, 38, -109, 71, 64, 7, 66, -110, 77, 28, -48, 1, -48, -102, 76, -48, -96, 3, -102, 34, 104, 81, 19, 64, 2, -123, 10, 34, 69, 48, 4, -47, 19, 66, -128, -96, 2, 62, -76, -103, -91, 30, -76, -104, -90, 0, 20, 98, -120, 80, 19, 72, 5, 77, 39, -54, -124, -51, 21, 0, 44, 80, -46, -117, 125, -23, 67, 90, 64, 24, 20, -80, 5, 36, 68, -46, -58, -76, -60, 18, -57, 113, 95, -34, 20, -108, -118, 91, -122, 19, 29, 85, -7, 82, 69, 0, 44, 82, -59, 32, 82, -59, 52, 38, 44, 82, -123, 32, 82, -22, -48, -123, 81, -118, 64, -91, 77, 82, 1, 84, 116, 83, 66, -104, -125, -25, 67, -107, 21, 0, 117, -92, 1, -125, 66, 104, -124, -64, -95, 53, 32, 40, 116, -93, -92, -47, -118, 96, 42, 69, 9, -94, -95, 62, 84, 8, 30, -15, -46, -114, -118, 104, 72, -102, 96, 15, 57, -94, -34, -124, -47, 122, -48, 5, 35, 119, -115, 56, -128, -76, 25, 73, -25, 75, 23, 41, -102, -95, -78, 37, 41, 82, 12, 127, 16, -4, -22, 88, -41, 104, -84, 104, -44, -79, 114, -19, 73, 0, 54, -62, -100, 81, -97, -68, 18, 7, -103, -2, 64, -46, 62, 42, -20, 110, -59, -78, -68, 3, -85, 7, -36, -93, -14, -88, 15, 57, -39, 50, -73, 114, 41, 121, 6, 108, -88, 65, 82, -107, -32, 0, -44, -97, 42, -120, -116, 102, -43, 87, 1, -91, 118, -19, 44, -126, 66, 87, 106, -24, 49, -44, -9, 96, 122, -47, 64, -109, 52, 45, 92, -123, -50, 102, -36, 105, 67, 112, -79, -89, -95, 26, 31, -44, -45, -95, -44, -98, -66, -43, 66, 47, -112, -30, 101, -112, -91, 13, -91, 64, -89, -24, 105, 11, 122, -31, 99, -25, 41, 7, -110, 52, -94, -128, -48, 41, 99, -81, -91, 37, 74, 51, 89, -26, -46, -92, -72, 12, -86, 100, 107, 52, -1, 0, 15, 37, 45, 96, -84, -74, -128, 18, -128, -91, -108, -92, 108, 37, 68, -97, -87, 38, -114, -128, -71, -111, 66, 105, -68, -38, 81, -51, 32, 28, -109, 67, 53, 34, 122, -5, 81, -125, 64, 14, 77, 2, 118, -13, 20, -119, -91, 125, -47, -3, -31, 52, 0, -70, 42, 41, 52, 38, -128, 12, -102, 20, -109, 66, -104, 10, -96, 77, 36, -102, 18, 104, 1, 83, 68, 72, -25, 73, -102, 19, 64, 7, 52, 71, 78, -108, 38, -123, 0, 20, -7, -47, -47, 81, -19, 69, -128, 41, 42, -93, -92, -109, -19, 64, 21, 92, 65, -60, -104, 95, 12, -39, 11, -84, 73, -41, 6, 109, 27, 101, -122, -13, -72, -28, 111, 3, 64, 7, -119, 32, 120, -51, 115, 59, -1, 0, -74, -53, -27, -87, 67, 9, -64, -20, -38, 108, 24, 11, -67, 121, 110, -85, -52, -124, 101, 3, -36, -7, -42, -17, -120, -101, -73, -65, -62, -18, 123, 118, -112, -76, 40, 110, -95, 48, 6, -43, -61, 113, -101, 70, -84, -18, 22, -106, -13, -72, -127, -79, 73, 76, 15, 45, -87, -82, -10...

and at end of this byte array im getting this 3 dots and array is not completed ,so how can i get correct byte array 

base64 string is

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAEgAcwDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD+wC2k
D/aRgsZBGA2OJRuthgfM5HcZYSlvMPE5n33aSAFBOfnUmIA5yZMG3BU7jIVwjDB

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

// convert from bitmap to byte array
public byte[] getBytesFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

// get the base 64 string
String imgString = Base64.encodeToString(getBytesFromBitmap(someImg), 
                       Base64.NO_WRAP);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...