2011-01-28 114 views
124

有人能告訴我將圖像(最大200KB)轉換爲Base64字符串的代碼嗎?如何將圖像轉換爲Base64字符串?

我需要知道如何使用android,因爲我必須添加功能來將圖像上傳到遠程服務器,在我的主應用程序中將它們放入數據庫的一行,作爲字符串。

我在谷歌和在StackOverflow搜索,但我找不到容易的例子,我可以 負擔得起,我也找到一些例子,但他們不談論轉換成字符串。然後我需要轉換成字符串以便通過JSON上傳到我的遠程服務器。

+4

@ user609239環斷 – rmirabelle 2014-10-12 22:43:27

回答

271

您可以使用Base64編碼的Android類:

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT); 

你有你的圖像轉換爲字節數組,雖然。這裏有一個例子:

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg"); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object 
byte[] b = baos.toByteArray(); 

*更新*

如果你使用的是老式的SDK庫(因爲你希望它與舊版本的操作系統在手機上工作),你不會有打包的Base64類(因爲它剛剛出現在API級別8中,也就是版本2.2)。

檢查本文出去變通:

http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

+0

感謝,並如何使byteArrayImage形成JPG? – NullPointerException 2011-01-28 16:52:57

+6

爲您更新了答案。 – xil3 2011-01-28 16:55:37

+0

好的,他們我可以把該字符串(encondedImage)與PHP + JSON的遠程數據庫列?這種類型必須是數據庫的列嗎? VARCHAR? – NullPointerException 2011-01-28 19:46:19

6

如果您需要在JSON的base64,檢查Jackson:它的二進制數據的讀/寫如在低級別的base64明確支持(JsonParser,JsonGenerator)和數據綁定級別。所以你可以擁有byte []屬性的POJO,並且編碼/解碼是自動處理的。 而且非常有效,如果這一點很重要。

84

而不是使用Bitmap,你也可以通過微不足道的InputStream來做到這一點。好吧,我不知道,但我覺得它有點高效

InputStream inputStream = new FileInputStream(fileName);//You can get an inputStream using any IO API 
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(); 
String encodedString = Base64.encodeToString(bytes, Base64.DEFAULT); 
3

這個代碼知府跑在我的項目

profile_image.buildDrawingCache(); 
       Bitmap bmap = profile_image.getDrawingCache(); 
       String encodedImageData =getEncoded64ImageStringFromBitmap(bmap); 


public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) { 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(CompressFormat.JPEG, 70, stream); 
    byte[] byteFormat = stream.toByteArray(); 
    // get the base 64 string 
    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP); 

    return imgString; 
} 
0
byte[] decodedString = Base64.decode(result.getBytes(), Base64.DEFAULT); 
3
// put the image file path into this method 
    public static String getFileToByte(String filePath){ 
     Bitmap bmp = null; 
     ByteArrayOutputStream bos = null; 
     byte[] bt = null; 
     String encodeString = null; 
     try{ 
     bmp = BitmapFactory.decodeFile(filePath); 
     bos = new ByteArrayOutputStream(); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
     bt = bos.toByteArray(); 
     encodeString = Base64.encodeToString(bt, Base64.DEFAULT); 
     }catch (Exception e){ 
     e.printStackTrace(); 
     } 
     return encodeString; 
} 
2

如果你在的Android這樣做,這裏是一個從React Native codebase複製的幫手:

import java.io.ByteArrayOutputStream; 
import java.io.Closeable; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 

import android.util.Base64; 
import android.util.Base64OutputStream; 
import android.util.Log; 

// You probably don't want to do this with large files 
// (will allocate a large string and can cause an OOM crash). 
private String readFileAsBase64String(String path) { 
    try { 
    InputStream is = new FileInputStream(path); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    Base64OutputStream b64os = new Base64OutputStream(baos, Base64.DEFAULT); 
    byte[] buffer = new byte[8192]; 
    int bytesRead; 
    try { 
     while ((bytesRead = is.read(buffer)) > -1) { 
     b64os.write(buffer, 0, bytesRead); 
     } 
     return baos.toString(); 
    } catch (IOException e) { 
     Log.e(TAG, "Cannot read file " + path, e); 
     // Or throw if you prefer 
     return ""; 
    } finally { 
     closeQuietly(is); 
     closeQuietly(b64os); // This also closes baos 
    } 
    } catch (FileNotFoundException e) { 
    Log.e(TAG, "File not found " + path, e); 
    // Or throw if you prefer 
    return ""; 
    } 
} 

private static void closeQuietly(Closeable closeable) { 
    try { 
    closeable.close(); 
    } catch (IOException e) { 
    } 
} 
-1

使用此代碼:

byte[ ] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT); 

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
0

下面是僞代碼,可以幫助你:

public String getBase64FromFile(String path) 
     { 
     Bitmap bmp = null; 
     ByteArrayOutputStream baos = null; 
     byte[] baat = null; 
     String encodeString = null; 
     try 
      { 
      bmp = BitmapFactory.decodeFile(path);  
      baos = new ByteArrayOutputStream();     
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      baat = baos.toByteArray(); 
      encodeString = Base64.encodeToString(baat, Base64.DEFAULT); 
      } 
      catch (Exception e) 
      { 
      e.printStackTrace(); 
      } 

     return encodeString; 
    } 
相關問題