2012-08-15 44 views
0

我試圖採取類似的圖像的路徑: 「內容://媒體/外部/圖像/媒體/ 3」以一個位圖圖像的路徑,將其轉化爲Base64編碼

和轉換它變成一個base64字符串。

這裏是我現在的代碼:

public String ConvertandSetImagetoBase64(String imagePath) { 
    String base64 = null; 
    byte[] input = null; 

    try{ 
     FileInputStream fd = new FileInputStream(imagePath); 
     Bitmap bmt = BitmapFactory.decodeFileDescriptor(fd.getFD()); 

     try{     
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      Bitmap tmp = ProfileActivity.scaleDownBitmap(bmt, 10, this); 
      tmp.compress(Bitmap.CompressFormat.JPEG, 10, stream); 
      input = stream.toByteArray(); 
      base64 = Base64.encodeToString(input, Base64.DEFAULT); 
      //LocalProfileActivity.input = input; 
     }catch(Exception e){ 
      Log.e(LOG_TAG,"[ONACTIVITYRESULT] Could not bind input to the bytearray: " + e.getMessage()); 
     } 
    } 
    catch (Exception e){ 
     Log.e("LocalProfile", "ConvertandSetImagetoBase64: Could not load selected profile image"); 
    } 
    return base64; 
} 

內容://媒體/外部/圖像/媒體/ 3是我傳遞什麼到方法。誰能幫我?

+0

由於你的鏈接的方案是'content'你或許應該使用[ContentResolver的#openInputStream(..) (http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri))而不是'FileInputStream'。 – Jens 2012-08-15 17:24:03

回答

0

因爲您指定的位置作爲一個URI,你可以嘗試像:

URL url = new URL(imagePath); 
Bitmap bmt = BitmapFactory.decodeStream(url.openStream());