web-dev-qa-db-ja.com

Androidで画像をバイト配列に変換し、バイト配列をbase64文字列に変換する方法は?

画像をバイト配列に変換し、そのバイト配列をbase64文字列に変換するコードを教えてください。適切な結果が得られない以下のコードを書きます。

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

得られた結果は

 [-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文字列は

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAEgAcwDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE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gurnani

これを試して:

// 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);
80
Dhruv Gairola

次のコードを記述して、sdcardからBase64エンコード文字列に画像を変換し、JSONオブジェクトとして送信します。

String filepath = "/sdcard/temp.png";
File imagefile = new File(filepath);
FileInputStream fis = null;
try {
    fis = new FileInputStream(imagefile);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
}

Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos);    
byte[] b = baos.toByteArray(); 
encImage = Base64.encodeToString(b, Base64.DEFAULT);
11
Priyank Desai

彼らはあなたの問題を解決するために必要なほとんどのものを包みました、 tests の1つはこのように見えます:

String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
String code = "background: url(folder.png);";

StringWriter writer = new StringWriter();
embedder = new CSSURLEmbedder(new StringReader(code), true);
embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));

String result = writer.toString();
assertEquals("background: url(" + folderDataURI + ");", result);
1

ここに別の解決策があります...

System.IO.Stream st = new System.IO.StreamReader (picturePath).BaseStream;
byte[] buffer = new byte[4096];

System.IO.MemoryStream m = new System.IO.MemoryStream ();
while (st.Read (buffer,0,buffer.Length) > 0) {
    m.Write (buffer, 0, buffer.Length);
}  
imgView.Tag = m.ToArray ();
st.Close ();
m.Close ();

それが役に立てば幸い!

1
user3219253

ファイルをbase64文字列に変換するには、この簡単なソリューションを試してください

String base64String = imageFileToByte(file);

public String imageFileToByte(File file){

    Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}
0
Vigneswaran A