2010-08-18 102 views
4

在我的應用程序中,我將base64字符串轉換爲image.For爲此我最初將base 64文件轉換爲字節數組,然後嘗試轉換爲圖像。 要轉換爲圖片我使用下面的代碼BitmapFactory.decodeByteArray在Android中返回null

File sdImageMainDirectory = new File("/data/data/com.ayansys.Base64trial"); 
    FileOutputStream fileOutputStream = null; 
    String nameFile="Images"; 
    try { 

     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize = 5; 
     options.inDither = true; 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); 


     fileOutputStream = new FileOutputStream(
       sdImageMainDirectory.toString() +"/" + nameFile + ".jpg"); 


     BufferedOutputStream bos = new BufferedOutputStream(
       fileOutputStream); 

     myImage.compress(CompressFormat.JPEG, quality, bos); 

     bos.flush(); 
     bos.close(); 

,但我讓我的形象,在空

Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); 

請讓我知道您的寶貴建議。

在此先感謝:)

+0

只是一個提示:留出一個空行之前和你的代碼後,使正確的代碼格式化 – WarrenFaith 2010-08-18 11:19:26

+0

@Remmyabhavan嗨,我遇到了同樣的問題,你可以建議我 – Pragnani 2013-02-04 12:03:09

回答

2

decodeByteArray不會將base 64編碼轉換爲字節數組。你需要首先使用Base64到字節陣列轉換器

+1

是的,我正在使用它將字節數組轉換爲位圖images.But我得到空錯誤。我傳遞的圖像數據是字節數組。 – Remmyabhavan 2010-08-18 12:16:55

1

我遇到了同樣的問題。我解決它通過消除字符串的這一部分:

「數據:圖像/ JPEG; BASE64,」