2013-02-12 29 views
1

在將JPEG轉換爲base64方面有很好的資源。如何在不解碼位圖的情況下將JPEG/PNG編碼爲Base64,使用更少的內存

我在這樣做而不解碼到bimap的,避免任何存儲器通貨膨脹特別感興趣。我也明白,任何文件可以通過將其轉換成字節數組首先被編碼成Base64編碼。

因此,如果我們可以直接創建JPEG/PNG文件的字節數組遠遠小於解碼JPEG位圖的字節數組,我們可以將其轉換爲使用較少的內存佔用的base64。

最接近的答案,我所遇到的就是這個https://stackoverflow.com/a/10160856/499752

+0

http://developer.android.com/reference/android/util/Base64OutputStream.html – Selvin 2013-02-12 21:33:29

回答

0
public void getGalleryDetails(String path) throws FileNotFoundException { 


    InputStream inputStream = new FileInputStream(path); 
    byte[] bytes; 
    byte[] buffer = new byte[8192]; 
    int bytesRead; 
    ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    try{ 
     while((bytesRead = inputStream.read(buffer)) != -1){ 
      output.write(buffer, 0, bytesRead); 
     } 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 

    bytes = output.toByteArray(); 

    encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT); 
    Log.i("ENCODED", encodedImage); 
} 

實際上,你可以使用這個......在這裏你可以提供文件的路徑轉換爲Base64編碼。對不起後期的帖子...只是說這個帖子。

相關問題