2017-03-08 275 views
1

我用下面的函數來我的base64字符串轉換爲位圖:皈依Base64編碼字符串爲位圖返回null

public void displayUserImage() { 
    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT); 

    Bitmap b = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    imageViewUser.setImageBitmap(b); 
} 

當我excecute這個功能我得到下面的輸出:

D/skia: --- SkAndroidCodec::NewFromStream returned null 

的base64字符串是:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8KCwkMEQ8SEhEPERATFhwXExQaFRARGCEYGhwdHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAGQAZADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAI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A/hTcm9jRUklY2LrX724RfmVMDBOOT71lyXdxJIJDO7Mpypz0NRowUEEZzTWYAnGKlsToxlFxa0OlttXGoQCC7YCQrtIJ+9TrFILK+2twrHAPp+NclJcIhBLbcdKntNdhmdYLiZeD8jY/TNZOx8dmuTOj+8o7dj1uxSE2vyOASOo61JY28ZnJdyAPWuK0XUJYSqM7PE3cN0rrrK7u4XBiJKOBwcVKj2PlW1B2mbeIXBTeDmm+Wu7aB37VLFeIQBMYTkc7hir8NrBcAuv2dcd1cZqeRhzxfUrQxqFGccetL9rIl2bFCA/erYsNKjkBZ8yIPQipLnTrAyjy4SrjqOuaLE1HFbGJdXCqCsC7j/ABOFrPXSWluRcTgxqwz7muh1SW1trR1kYRke3FeS/Eb4oR2EjaRoAW4vSNpl/hT/ABotfQ55SVjvdRurW0IhiGHPfvVa1kiXLFgzE814v4a069ub7+1NY1Seadm3lVkI5969LtLxdq4JNdEKfLuYuVz/2Q== 

有人可以告訴我爲什麼這不起作用嗎?

+0

同時顯示代碼帽將位圖作爲base64編碼。 – greenapps

+0

您應該將字符串更改爲字節並且不傳遞數據圖像並僅傳遞圖像字節 –

+0

您從哪裏獲取encodedImage? – greenapps

回答

1

您必須從base64字符串中刪除前導data:image/jpeg;base64,

public void displayUserImage() { 
    byte[] decodedString = Base64.decode(encodedImage.split(",")[1], Base64.DEFAULT); 

    Bitmap b = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    imageViewUser.setImageBitmap(b); 
} 

應該工作。

但是,當然,你應該經常檢查你的base64字符串是否包含這些信息,然後刪除它,否則就很好。